Response Control for Iphone Development (3) buttons

Source: Internet
Author: User

The last time we introduced the implementation principle of helloWorld, most of the Code is automatically generated by the project template. Today we are going to study how to add button and button listening to Iphone applications, first, create a project:



 

Then open the xib file in the automatically generated file and perform the following operations. Drag a label and two buttons and send the content as follows:




 

Now we need to use a new concept, that is, the output port and behavior. The keyword of the output port is IBOutlet. This is just a simple keyword, but in xcode development, variables declared using this keyword can be associated with the control drawn from the xib file. It is a bit similar to findViewById in android development. The behavior keyword is IBAction. this keyword is used to modify the method. The modifier can be used to bind various events of the control in the xib file. The custom method modified by IBAction has a parameter sender, it can be used to pass some properties of the control in xib, such as the title or something. Now, let's look at the Code. In viewController. h, use IBOutlet to declare the label and two buttons, as well as the response method modified with IBAction.

ViewController. h:



 

The small circle on the left indicates that it can be connected to the control in the xib file, but it is hollow, indicating that it is not connected yet. We will implement it in viewController. m below:

ViewCotroller. m:


[Plain] # import "ViewController. h"
 
@ Interface ViewController ()
 
@ End
 
@ Implementation ViewController
@ Synthesize label;
@ Synthesize bu1;
@ Synthesize bu2;
-(IBAction) click :( id) sender
{
Label. text = [sender titleForState: UIControlStateNormal]; // 1
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
 
-(Void) viewDidUnload
{
[Super viewDidUnload];
// Release any retained subviews of the main view.
}
 
-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}
 
@ End
# Import "ViewController. h"

@ Interface ViewController ()

@ End

@ Implementation ViewController
@ Synthesize label;
@ Synthesize bu1;
@ Synthesize bu2;
-(IBAction) click :( id) sender
{
Label. text = [sender titleForState: UIControlStateNormal]; // 1
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}

-(Void) viewDidUnload
{
[Super viewDidUnload];
// Release any retained subviews of the main view.
}

-(BOOL) shouldAutorotateToInterfaceOrientation :( UIInterfaceOrientation) interfaceOrientation
{
Return (interfaceOrientation! = UIInterfaceOrientationPortraitUpsideDown );
}

@ End: sender in code 1 refers to the control that triggers this method. titleForState is used to retrieve the title of this control. In normal conditions, buttons and other controls are normal, press status and other differences. Obtain the title and assign it to the label. In this way, you can click a different button to change the label content.

In addition, instances of the xib File and viewController class are connected through interfaceBuilder. The xib File built by interfaceBuilder contains a File's Owner, which is actually an instance of the viewController class, controls data and views through viewController instances. Now, we need to associate view with viewController. The method is very simple:

Step 1: Associate the view represented by the xib File with the viewController represented by the File's Owner. You only need to press the Control key on the xib interface, click the File's Owner and drag it to the View.



 

Step 2: connect the three controls in the view with the variables declared by the three output ports in the viewController, as shown in. When you select a control in the xib, A right arrow icon in the upper right corner indicates that a new referencing outlet will appear in connection insector (connection check). Click the small circle on the right to drag the File's Owner, drag the control name defined in viewController to the File's Owner and select it. Bu1 and bu2 are the same.




 

In this way, the three controls are bound to the controls in viewController. You and I can operate on them, but now we only bind controls, the click method of the button has not been bound to the button. In this case, we need to click bu1 and we will find a column on the right, such as sent Events, representing some Events of the button, there is a touch up inside, indicating that when the finger leaves the screen, the last contact position is on the button, that is, the method of clicking, click the small circle on the right and drag it to the File's Owner. In the displayed list, select the defined click method. The same applies to bu2.



 

In this case, all controls and response methods are mapped from the xib file to the viewController object. When we click different buttons, the content of the button is assigned to the label based on the sender's title. Let's witness the miracle together:



 

Isn't it easy? The base layer still shows the MVC mode. The controller connects the view and the model, while the view and data are always lost.

 

From the holydancer Column
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.