Detail the IBOutlet and IBAction when iOS applications use the Storyboard layout

  • 2020-05-27 07:13:49
  • OfStack

In graphical interface programming, the first problem to be solved is how do you associate a static interface with code, or how does code relate to objects on the interface

Communication, how the code operates on objects on the interface. On the iPhone platform, IBOutlet and IBAction are introduced. By adding IBOutlet before the variable

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

IBOutlet and IBAction are illustrated below with an example of connecting to a web server (NetworkConnection).

The interface has Text Field UI objects of host and port, and one Button object.

Therefore, the code needs to define two IBOutlet variables, which are used to define host and port respectively. An IBAction method is used to initiate the join action.

In the NetworkConnectionViewController.h file:

Define variables:


@interface NetworkConnectionViewController : UIViewController {
    UITextField *host;
    UITextField *port;
}

These two variables are explained as IBOutlet variable:

@property(nonatomic, retain) IBOutlet UITextField *host;
@property(nonatomic, retain) IBOutlet UITextField *port;

Add in NetworkConnectionViewController.m:


@synthesize host;
@synthesize port;

Open the NetworkConnectionViewController.xib file and drag two Text Field objects onto it.

Hold down Ctrl and drag File's Owner above Text Field. Outlets selection list will pop up. host and port can be seen in the list.

Select the Outlet variable for the two Text Field, respectively. When 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 shows up on the interface.

To verify that the variable is associated with the interface object, the viewDidLoad method values the variable and compiles it.


- (void)viewDidLoad
{
    [super viewDidLoad];
    host.text = @"192.168.1.100";
    port.text = @"8080";
}

When run, you can see these values in Text Field of the interface, indicating that the variables are correctly associated with the interface objects. You can then see the value of the variable in the interface.

Add an IBAction method to the NetworkConnectionViewController.h file:


-(IBAction)connectNetwork;
in NetworkConnectionViewController.m Implement the method in the file: -(IBAction)connectNetwork {    UIAlertView *alter = [[UIAlertView alloc] initWithTitle: @"Connection Network" message: @"sending command to the server" delegate: self cancelButtonTitle: @"OK" otherButtonTitles: nil];
   
    [alter show];
    [alter release];
//connect network
//............
}

Open NetworkConnectionViewController.xib and drag 1 Round Rect Button onto it.
Then hold down Ctrl and drag the button to File's Owner in the IBAction list that pops up

Choose connectNetwork. The connectNetwork method is then called when the button is pressed and bounced.

IBOutlet and IBAction are the foundation of the application development of iPhone and the first step towards the application development of iPhone platform.

Why is the IBOutlet property weak?

Because when we control will drag Storyboard, had one of the newly created object, the object is added to the view controller view, one view subViews attribute, this property is an array, it is the view all child view, and we add the control in the array, so that we actually control object belongs to view, i.e. view is strong references to add to it controls. When we use Outlet property, we are using viewController, and this Outlet property is strongly referenced by view. We only use it in viewController, we don't need to own it, so it is weak.

If you change weak to strong, this is fine and does not result in a strong reference loop. When an viewController pointer points to another object or is nil, the viewController is destroyed, and one less strong reference pointer is needed to the control. Then its view is destroyed, subViews is no longer there, and the control loses another strong reference pointer. If there are no other strong references, the control will be destroyed as well.

However, since you don't have to set the Outlet property to strong, weak is fine:]

A control can have multiple Outlet properties in viewController, which is equivalent to an object and can have multiple Pointers to it (multiple references).

However, one Outlet property can only correspond to one control, that is, if there are button1 and button2, button1 has an Outlet property named button in viewController, then button points to button1, but if button2 is used to reassign button, then button points to button2. In other words, the later overrides the original.

One control can trigger multiple IBAction within viewController. For example, if there is an button control, and there are several methods in viewController, then clicking button will trigger all of these methods.

If I have more than one control, such as button1, button2, button3, they can also bind a buttonClick method at the same time, either click button1, button2 or button3, will trigger the buttonClick method.

The above said, button1 button2, button3 could trigger buttonClick method, if you want to be inside the buttonClick method to distinguish which is it button trigger may have several different way.

You can set one Outlet property for each of the three button, and then determine which sender and Outlet properties are the same object in buttonClick, so that you can distinguish them. Obviously, this doesn't make sense, because the three properties you created are wasteful.
We can add one tag to each of the three button, and pass switch(or if...) inside buttonClick. Determine whether the tag of sender and the tag added to each button are 1 and, if 1 is 1, the same object.


Related articles: