Explain the Iboutlet and Ibaction_ios when using storyboard layout for IOS applications

Source: Internet
Author: User

In the graphical interface programming, the first problem to solve is how to associate the static interface with the code, or how the code and the object on the interface

Communication, how the code operates on an object on the interface. On the iphone platform, Iboutlet and ibaction are introduced. By adding Iboutlet before a variable

To show that the variable will correspond to a UI object on the interface, add Ibaction before the method to show that the method will correspond to the events on the interface.

The following is an example of connecting a network server (networkconnection) to illustrate Iboutlet and ibaction.

There is a text Field UI object with host and port on the interface, a button object.

So the code needs to define two Iboutlet variables, respectively, to define host and port; A ibaction method used to initiate the connection action.

In the NetworkConnectionViewController.h file:

To define a variable:

Copy Code code as follows:

@interface Networkconnectionviewcontroller:uiviewcontroller {
Uitextfield *host;
Uitextfield *port;
}

Describe these two variables as Iboutlet variables:
Copy Code code as follows:

@property (nonatomic, retain) Iboutlet Uitextfield *host;
@property (nonatomic, retain) Iboutlet Uitextfield *port;

Add in NETWORKCONNECTIONVIEWCONTROLLER.M file:

Copy Code code as follows:

@synthesize host;
@synthesize Port;

Open the Networkconnectionviewcontroller.xib file and drag the two text field object to the top.

Holding down the CTRL key and dragging the file ' s owner to the text field pops up the outlets selection list, where you can see the host and port.

Select the outlet variable for the two text field, respectively. When you do this, the Text Field object on the interface is associated with the variables defined in the program,

When you change the properties of a variable, it appears in the interface.

To verify that a variable is associated with an interface object, pay the variable in the Viewdidload method and then compile and run.

Copy Code code as follows:

-(void) viewdidload
{
[Super Viewdidload];
Host.text = @ "192.168.1.100";
Port.text = @ "8080";
}

After running, you can see these values in the text field of the interface, indicating that the variables are associated correctly with the interface object. So you can see the value of the variable in the interface.

Add a Ibaction method to the NetworkConnectionViewController.h file:

Copy Code code as follows:

-(ibaction) connectnetwork;
Implement this method in the Networkconnectionviewcontroller.m file:

-(Ibaction) connectnetwork

Uialertview *alter = [[Uialertview alloc] Initwithtitle: @ "Connection Network" message: @ "Sending command to the server" D Elegate:self cancelbuttontitle: @ "OK" otherbuttontitles:nil];

[Alter show];
[Alter release];
Connect Network
//............


Open the Networkconnectionviewcontroller.xib and drag a round Rect button to the top.
Then hold down the CTRL key and drag the button to file ' owner, in the pop-up ibaction list

Select Connectnetwork. This will invoke the Connectnetwork method when the button is pressed and bounced.

Iboutlet and Ibaction are the foundation of iphone application development and the first step in the successful development of iphone platform applications.

Why is the Iboutlet attribute weak?

Because when we drag the control onto the storyboard, the equivalent of creating an object that is added to the view of the views controller, view has a Subviews property, which is an array that contains all of the view's child view, And the controls we add are in this array, so it turns out that our control object is actually a view, which means the control that the view pair adds to it is a strong reference. When we use the outlet attribute, we use it in the Viewcontroller, and the outlet attribute has a view for strong reference, we are only using it in Viewcontroller, and it is not necessary to have it, so it is weak.

If you change the weak to strong, there is no problem, and it does not cause a strong reference loop. When the Viewcontroller pointer points to another object or is nil, the Viewcontroller is destroyed, and a strong reference pointer is missing for the control. Then its view is also destroyed, then the subviews does not exist, then the control is less a strong reference pointer, if there is no other strong reference, then the control will be destroyed.

However, since there is no bound outlet property set to strong, then the use of weak is good:]

A control can have more than one outlet property in the Viewcontroller, which is equivalent to an object that can have multiple pointers to it (multiple references).

However, a outlet property can only correspond to one control, that is, if there is a outlet property named button in the Viewcontroller of Button1 and Button2,button1, then the button points to Button1, But if you assign a value to the button with Button2, then the button points to button2. That is to say, the later covers the original.

A control can trigger multiple ibaction inside the Viewcontroller. For example, there is a button control, there are several methods inside the Viewcontroller, then clicking the button will trigger all of these methods.

If I have multiple controls, such as Button1,button2,button3, they can also bind to a ButtonClick method, which is triggered by clicking either Button1,button2 or Button3.

It says that Button1,button2,button3 may all trigger the ButtonClick method, and there are several possible ways to distinguish which button is triggered in the ButtonClick method.

You can set a outlet property for each of these three button, and then in ButtonClick, you can distinguish between sender and which outlet attribute is the same object. But obviously, this is not reasonable, because the three attributes created are a bit wasteful.
We can add a tag to each of the three button, and ButtonClick inside the switch (or if ...) Determine whether the sender tag is consistent with the tag added to each button, and if consistent, the same object.

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.