Learn Swift from scratch (Day 64) -- objective and action of Cocoa Touch Design Model and Application
Target and Action are the event processing mechanisms in iOS and OS X application development.
Problem proposal
Is a ButtonLabelSample case design prototype diagram, which contains a tag and a button. When you click a button, the Label text will be replaced with HelloWorld from the initial Label.
In the ButtonLabelSample case, the first problem to be solved is: who is responsible for responding to the event after the event is clicked? Who handles the event? To answer this question, open the Main. storyboard, And the OK button is in the Main. the storyboard defines that the response event and event processing should be in the program code ViewController. implemented by swift. So how to associate the "OK" button click event with the event processing code in ViewController. swift? Different computer languages are implemented in different ways. In iOS and OS X application development, event processing is implemented through the objective and action mechanisms.
Solution
Buttons and other controls are inherited from the UIControl class and have some advanced events. The goal and action mechanism are to associate special control events with methods in the View Controller (or view, this process is called "Defining Action events ". The "target" is the response event object. To facilitate access to the status of other controls, this object is generally a View Controller (or view ). An action is a control event.
In the ButtonLabelSample case, the button is defined in the storyboard file (or Xib file), and the response button click event (Action) is defined in the View Controller (target) method, as shown in, the target and action are connected by defining action events.
There are two ways to achieve the connection between the target and the Action: InterfaceBuilder connection implementation and programming implementation.
1. Interface Builder connection implementation
The implementation of InterfaceBuilder connection is a reality in the storyboard or Xib file.
2. Programming implementation
The programming implementation is implemented through the addTarget (_: action: forControlEvents :) method of the UIControl class. The main code is as follows:
Class ViewController: UIViewController {override func viewDidLoad () {super. viewDidLoad () self. view. backgroundColor = UIColor. whiteColor () let screen = UIScreen. mainScreen (). bounds; let labelWidth: CGFloat = 90 let labelHeight: CGFloat = 20 let labelTopView: CGFloat = 150 let label = UILabel (frame: CGRectMake (screen. size. width rows-labelWidth)/2, labelTopView, labelWidth, labelHeight) label. text = "Label" // the label in the font. textAlignment =. center self. view. addSubview (label) let button = UIButton (type: UIButtonType. system) // create the UIButton object button. setTitle ("OK", forState: UIControlState. normal) let buttonWidth: CGFloat = 60 let buttonHeight: CGFloat = 20 let buttonTopView: CGFloat = 240 button. frame = CGRectMake (screen. size. width rows-buttonWidth)/2, buttonTopView, buttonWidth, buttonHeight) button. addTarget (self, action: "onClick:", role forControlEvents: UIControlEvents. touchUpInside) self. view. addSubview (button)} func onClick (sender: AnyObject) {NSLog ("OK Button onClick. ")}...}
In the above Code, create and set the UIButton object, and create the UIButton object. The parameter type is the style of the set button. UIButton style:
-
Custom. Custom type. If you do not like the rounded corner button, you can use this type.
-
System. The default Property indicates that the button does not have a border. The button is a rounded rectangle by default before iOS 7.
-
Detail Disclosure. The detail display button is mainly used for detail display in the table view.
-
Info Light and Info Dark. These two buttons are information buttons. The style is the same as the detail display button, indicating that some information needs to be displayed or some content can be set.
-
Add Contact. Add contact button.
The Code calls the addTarget (_: action: forControlEvents :) method. The first parameter of the method is the target, which is the event processing object. In this example, It is self. The second parameter of the method is action, that is, the method in the event processing object,
The code is "onClick:". The third parameter of the method is the event, and the TouchUpInside event is the button's touch click event.
If you call the following method without parameters:
func onClick() {}
The call code is as follows:
button.addTarget(self,action: "onClick", ê forControlEvents:UIControlEvents.TouchUpInside)
The difference is that the method name of the action parameter "onClick" is different. The colon of the Method Name of the action parameter implies that the method name should have several parameters. If the method to be called is in the following three parameter formats:
func onClick(sender: AnyObject, forEvent event: UIEvent) {}
The call code is as follows:
button.addTarget(self,action: "onClick:forEvent:", ê forControlEvents:UIControlEvents.TouchUpInside)
Here, "onClick: forEvent:" indicates the name of the called method, while onClick indicates that the method name is the same, and forEvent indicates the External Parameter Name of the second parameter.