I think everyone has read the basic tutorial on iPhone 4 and ipad2 development. xcode in this book is different from the latest version. With view base application removed, only single view application is similar.
First, create a single view application. Note that we do not need to reference the Count automatically here.
Click MainStoryboard_iphone.storyboard in the project list to edit the interface:
We create two buttons and a blank text for output:
Directory structure and view structure:
2. Then we write code to connect to the IB Interface Editor: here we can think that IBOutlet is the output of interaction with IB, while IBAction is the event of interaction with IB.
Compile ViewContoller. h:
# Import <UIKit/UIKit. h>
@ Interface ViewController: UIViewController
{
}
@ Property (nonatomic, retain) IBOutlet UILabel * statusText;
-(IBAction) buttonPressed :( id) sender;
@ End
# Import <UIKit/UIKit. h> @ interface ViewController: UIViewController {}@ property (nonatomic, retain) IBOutlet UILabel * statusText;-(IBAction) buttonPressed :( id) sender; @ end
Then implement the function in ViewContoller. m:
# Import "ViewController. h"
@ Implementation ViewController
@ Synthesize statusText;
-(IBAction) buttonPressed :( id) sender
{
NSString * title = [sender titleForState: UIControlStateNormal]; // sender is used to obtain the text titleForState of Different buttons. It is a function to obtain text based on the button status.
NSString * newText = [[NSString alloc] initWithFormat: @ "% @ button pressed.", title]; // Add the text in the title to newText.
StatusText. text = newText;
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(Void) viewDidUnload
{
[Super viewDidUnload];
Self. statusText = nil;
// Release any retained subviews of the main view.
}
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
If ([[UIDevice currentDevice] userInterfaceIdiom] = UIUserInterfaceIdiomPhone ){
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
} Else {
Return YES;
}
}
-(Void) dealloc
{
[StatusText release];
[Super dealloc];
}
@ End
# Import "ViewController. h "@ implementation ViewController @ synthesize statusText;-(IBAction) buttonPressed :( id) sender {NSString * title = [sender titleForState: UIControlStateNormal]; // sender is used to obtain the titleForState text of Different buttons. The function NSString * newText = [[NSString alloc] initWithFormat: @ "% @ button pressed is used to obtain the text of Different buttons. ", title]; // put the text in the title into newText statusText. text = newText;}-(void) viewDidLoad {[super ViewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} -(void) viewDidUnload {[super viewDidUnload]; self. statusText = nil; // Release any retained subviews of the main view .} -(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation {if ([[UIDevice currentDevice] userInterfaceIdiom] = UIUserInterfaceIdiomPhone) {return (inte RfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown);} else {return YES;}-(void) dealloc {[statusText release]; [super dealloc];} @ end
Then we connect the Code with IB:
Select MainStoryboard_iphone.storyboard, right-click Buton in View Controller Scene, and select Touch down and
BottonPressed:
Connect lable and statusText in the following link:
Finally, run the program and click botton to see the effect:
From Henry Morgan's column