Declaring the Action Method

- (IBAction)buttonPressed:(id)sender

Adding Actions and Outlets to the Implementation file

the class's implementation file, Button_FunViewController.m

The file should look like something like this.

#import "Button_FunViewController.h"

@implementation Button_FunViewController

- (void)didReceiveMemoryWarning {

  // Releases the view if it doesn't have a superview.

  [super didReceiveMemoryWarining];

  // Release any cached data, images, etc that aren't in use.

}

- (void)viewDidUnload {

  //Release any retained subviews of the main view.

  //e.g.self.myOutlet = nil;

}

- (void)dealloc {

  [super dealloc];

}

@end

#import "Button_FunViewController.h"

@implementation Button_FunViewControoler

@synthesize  statusText;

- (IBAction)buttonPressed:(id)sender {

  NSString *title = [sender titleForState:UIControlStateNormal];

  NSString *newText = [[NSString alloc] initWithFormat:@"%@ button pressed.",title];

  statusText.text = newText;

  [newText release];

}

- (void)viewDidUnload{

  self.statusText = nil;

}

@synthesize statusText;

tell the compiler to automatically create  the accessor and mutator methods for us.

statusText and setStatusText

-(IBAction)buttonPressed: (id)sender {

  NSString *title = [sender titleForState:UIControlStateNormal];

  NSString *newText = [[NSString alloc] initWithFormat:

              @"%@ button pressed.",title];

  statusText.text = newText;

  [newText release];

}

- (void)dealloc {

  [statusText release];

  [super dealloc]

}

@end

the parameter passed into  an action method is the control and object  that invoke it.

sender will always point to the button that we are tapped.

one action method handle  the input from multipe controls,and we tell them apart by looking  at sender.

NSString *title = [sender titleForState:UIControlStateNormal];

We needed to provide a contol sate when we requested the buton's title.

The four possible state are normal,which represents the control when

it's active but not currently being used,higlighted,

in the process of being tapped or otherwise used;

disabled  that is not enabled and cann't be used.

selected certain controls has and which indicates that the control is curretly selected.

UIControlStateNormal repeasents a control's normal state and is

the one you will use the vast majority of the time.

If values for the other states are not specified, those states will have the same value as the mornal state.

NSString *newText  = [[NSString alloc] initWithFormat:

            @"%@ button pressed.",title];

append the text "buton pressed." to the name of the button.

so if we tapped a button with a title of "Left"

statusText.text = newText;

[statusText setText:newText];

[newText:release];

The importance of releasing objects when you're finished with them cannot be 

overstated.

IOS device are very resource-constrained and even a small numbr of memory leaks ca 

cause your program to crash.  

NSString *newText = [NSString stringWithFormat:

            @"%@ button pressed.", title];

convenience or factory methods and they return an autoreleased object.

the general memory rule that if you didn't allocate it or retain it ,don't release it.

a cost associated with these convenience methods beause they use the autorelease pool.

viewDidUnload 

self.statusText = nil;

you need to set any outlets your class hs to nil in viewDidUnload.

release the outlet in our dealloc method

[statusText realse];

releasing this item might seem strange.  You might be thinking ,since we didn't instantiate it ,

we shouldn't be responsile for releasing it.

plain wrong 

we implemented properties for each of thease outlets  and specified retan in 

that property's attribute,release it is correct and nesserary.

Interface Builder will use our generated mutator method when assigning  the oulets

and that mutator will retain the object that is assigned to it ,so it's important 

to release the outlet here to avoid leaking memeory.

after releasing statusText by calling its release method by calling its elease method

our object is actually left in an invalid state for a brief peroid of time 

since the statusText variable still contains a pointer to the object we just released,which may very well not

not exist anymore

cause a crash.Also,in a mulitithreaded application,you might have a bug where an boject

that is being dealloced in one thread is simulaneouly eing accessed by aother,

which could also lead to creashes,deadlocks,or other kinds of 

misbehavior.

We could work around this by setting the pointer to nil in the dealloc method, but

that approach has potential problems of its own.

Using the Application Delegate

The other two files in the Classes folder implement our application delegate.

Cocoa Touch makes exensive use of delegates, which are classes that take responsibility for 

doing certain tasks on behalf of another object.

let us do things at certain perdefined times on ehalf of the UIApplication classs

Every iPhone appliction has one and only one instance of UIApplicaion,

whiich is responsible for the application's run loop and handles appliation-level

functionality uch as routing input to the appropriate class.

UIApplication is standard part of the UIKit, and it does its job mostly behind the scenes,

so you don't need to worry about it for the most part

can specific delegate methods.

you have code that needs to fire just before your program quits,

you would implement te method applicationWillTerminate

Click Button_FunAppDelegate.h

in the Groups & files pane t see the application delegate's header file.

#import <UIKit/UIKit.h>

@class  Button_FunViewController;

@interface Button_FunAppDelegate : NSObject <UIApplicationDelegate> {

  UIWindow *window;

  Button_FunViewController *viewController;

}

@property (nonatomic, retain) IBOutlet UIWindow *window;

@property(nonatomic, retain) IBOutlet Button_FunViewController *viewController;

@end

option methods.

#import "Button_FunAppDelegate.h"

#import "Button_FunViewController.h"

@implementation Button_FunAppDelegate

@synthesize window;

@synthesize viewController;

- (BOOL)apllication:(UIAplication *)application

  didFinishLauchingWithOptions:(NSDictionary *)launchOptions {

  [window addSubview:vieController.view];

  [window makeKeyAndVisible];

  return YES;

}

- (void)applicationWillTerminate:(UIApplication *)application {

  //

}

- (void)dealloc {

  [window release];

  [viewController release];

  [super dealloc];

}

@end

application:didFinishLanuchingWithOptions

adds our view controller's view as a subview to the application's main

window and make the window visible,which is how the view we

are going to design gets shown to the user.

Editing MainWindow.xib

Resources tab. We looked at the equivalent of Button_Fun-info.plst

when we added our icon to the project, and we looked at the equivalent of Buton_FunViewController.xib when we added out "Hello,World!" label.

in the Resources tab that we want to talk about.

MainWindow.xib is what causes your application's delegate,

main window, and view controller instances to get created at runtime.

the file is provided as part of the project emplate.

You don't need to change or do anything here.

a chance to see what's going on behind the scenes, to get a alimpse of the big pictue.

You should recognize the first two icons in this window from chapter2

the third icon is an instance of Button_FunAppDelegate.

Button_FunViewController

out application's one and only window.

once the nib file is loaded, our application will have one instance of our view controller,button_FunViewController; and 

one instance of UIWindow

create instance of other classes as well.

This is an incredibly powerfull feature.

Editing Button_FunViwController.xib

bring them all together, let's go ahead and construct our interface.

Creating the View in Interface Builder

the library is visible select the Library from the Tools menu.

View window is open.double click the icon called View in the nib's 

main window

construct our interface

Creating the View in Interface Builder

Drag a label from the library over to the view window.

Place the labe toward the bottom of the view, so the label lines up with the left and bottm blue 

guidelines 

The little blue guidelines are there to help you stick to the Aplle Huma Ierface Guidelines

HIG.

The inspector's text alignment buttons

click to select it ,and press c1 to bring up the inspector.

Change the text alignment to centered by Using the text alignment

buttons on the inspectore.

double-click the label, and delete the existing text.  We don't 

want an text to display until a button has been tapped.

Connecting Everything

make a connection from File;

The first step is to make a connection from File's Owner

to the label in the View window.Why File's Owner?

When an instance of UIViewController or one of its subclasses is 

instatitiated ,

it can be told to initialize interself from a nib.

In the template we' ve used, the Bugtton_FunViewController class will be loaded from the nib 

file

Button_FunViewController.xib.

it's part of the project template we chose.

Since the MainWindow.xib file contains an icon that represents 

Button_FunViewCotroller.

Connecting Outlets

原文地址:https://www.cnblogs.com/yushunwu/p/2677376.html