One of the basics of iphone app development: Iboutlet and Ibaction

Source: Internet
Author: User

When programming in the graphical interface, the first problem solved is how to associate the static interface with the code, or how the code relates to the object on the interface.

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

To indicate that the variable will correspond to a UI object on the interface, and add Ibaction before the method to indicate that the method will correspond to an event on the interface.

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

The interface has the text Field UI object of host and port, a button object.

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

In the Networkconnectionviewcontroller.h file:

Define variables:

@interface Networkconnectionviewcontroller: Uiviewcontroller {
    Uitextfield *host;
    Uitextfield *port;


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

In the Networkconnectionviewcontroller.m file, add:

@synthesize host;
@synthesize Port;


Openthe Networkconnectionviewcon troller.xib file and drag the two text field object to the top.

Holding down the CTRL key, drag the file's owner to the text field, and the Outlets selection list pops up and the host and port are visible in the list.

Select the outlet variable for two text field, respectively. Once this is done, 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 on the interface.

In order to verify that the variable is associated with an interface object, the variable is paid in the Viewdidload method and then compiled and run.

-(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, stating that the variables are associated correctly with the interface object. The value of the variable can then be seen in the interface.

Add a Ibaction method to the Networkconnectionviewcon troller.h file:

-(ibaction) connectnetwork;

implement this method in the Networkconnectionviewcon troller.m file:

-(Ibaction) connectnetwork

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

[Alter show];
[alter release];

Connect Network

//............

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

Select Connectnetwork. This invokes the Connectnetwork method when the button is pressed to bounce.

Iboutlet and ibaction are the basis for iphone application development and are the first step towards successful application development of the iphone platform.

One of the basics of iphone app development: Iboutlet and Ibaction

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.