When programming a graphical interface, the first problem solved is how to associate a static interface with the code, or how the code is with objects on the interface
Communication, how code manipulates objects on the interface. On the iPhone platform, IBOutlet and IBAction were introduced. By adding IBOutlet before variable
To illustrate that the variable will correspond to a UI object on the interface, add IBAction before the method to illustrate that the method will correspond to events on the interface.
The following is an example of connecting to 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.
Therefore, two IBOutlet variables need to be defined in the code, which are used to define host and port; an IBAction method, which is used to initiate the connection action.
In the file:
Define variables:
@interface NetworkConnectionViewController : UIViewController {
UITextField *host;
UITextField *port;
}
Describe these two variables as IBOutlet variables:
@property(nonatomic, retain) IBOutlet UITextField *host;
@property(nonatomic, retain) IBOutlet UITextField *port;
Add to the file:
@synthesize host;
@synthesize port;
Open the file and drag two Text Field objects on it.
Hold down the Ctrl key, drag File's Owner onto the Text Field, and the Outlets selection list will pop up, and you can see the host and port in the list.
Select Outlet variables for two Text Fields respectively. After doing this, the Text Field object on the interface is associated with the variables defined in the program.
When changing the properties of a variable, it will appear on the interface.
To check whether the variable is associated with the interface object, the variable is valued in the viewDidLoad method and then compile and run.
- (void)viewDidLoad
{
[super viewDidLoad];
= @"192.168.1.100";
= @"8080";
}
After running, you can see these values in the Text Field of the interface, indicating that the variable is correctly associated with the interface object. This allows you to see the value of the variable in the interface.
Add an IBAction method to the file:
-(IBAction)connectNetwork;
Implement this method in a 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 and drag a Round Rect Button to it.
Then hold down the Ctrl key, drag the button to File's Owner, and in the pop-up IBAction list
Select connectNetwork. In this way, when the button is pressed and pops up, the connectNetwork method will be called.
IBOutlet and IBAction are the foundation of iPhone application development and the first step in successfully moving towards iPhone platform application development.
Why is the IBOutlet property weak?
Because when we drag the control to the Storyboard, it is equivalent to creating a new object, and this object is added to the view controller's view. The view has a subViews property, which is an array, which contains all the subviews of the view, and the control we added is located in this array. This means that our control object actually belongs to the view, which means that the view has a strong reference to the control added to it. When we use the Outlet property, we use it in the viewController, and this Outlet property has a view for strong reference. We only use it in the viewController, and there is no need to have it, so it is weak.
If weak is changed to strong, there is no problem and will not cause a strong reference cycle. When the pointer of the viewController points to another object or is nil, the viewController is destroyed, and a strong reference pointer is missing to the control. Then its view is destroyed, and subViews no longer exists, and the control will lose another strong reference pointer. If there are no other strong references, the control will also be destroyed.
However, since the Outlet property is not necessary to set to strong, just use weak: ]
A control can have multiple Outlet properties in the viewController, which is equivalent to an object, and can have multiple pointers pointing to it (multiple references).
However, an Outlet property can only correspond to one control. That is to say, if there are button1 and button2, button1 has an Outlet property named button in the viewController. At this time, button points to button1, but if button2 is used to reassign the button, then button points to button2 at this time. In other words, the later one covered the original one.
A control can trigger multiple IBActions in the viewController. For example, there is a button control with several methods in the viewController. Clicking on button will trigger all these methods.
If I have multiple controls, such as button1, button2, button3, they can also bind a buttonClick method at the same time. Whether I click button1, button2 or button3, this buttonClick method will be triggered.
As mentioned above, button1, button2, button3 may all trigger the buttonClick method. If you want to distinguish which button triggered it in the buttonClick method, there may be several ways to do it.
You can set an Outlet attribute for each of these three buttons, and then determine which Outlet attribute sender and which Outlet attribute is the same object in buttonClick, so that you can distinguish it. But it is obvious that this is not reasonable because the three attributes created are a bit wasteful.
We can add a tag to each of the three buttons, and use switch (or if...) to determine whether the sender tag and the tag added to each button are consistent. If it is consistent, it is the same object.