"Learn Swift from scratch" study notes (Day 64) - Cocoa Touch design pattern and application goals and actions

Original article, welcome to reprint. Please indicate: Guan Dongsheng's blog

 

Target and Action are event handling mechanisms in iOS and OS X application development.

 

Questions raised

As shown in the figure is a ButtonLabelSample case design prototype diagram, which contains a label and a button. When the button is clicked, the label text will be replaced from the initial Label to HelloWorld .



 

 

The first problem to be solved in the ButtonLabelSample case is: who is responsible for responding to the button click event? Who does event handling? To answer this question, you can open the ButtonLabelSample case storyboard file Main.storyboard , as shown in the figure, the OK button is defined in the storyboard file Main.storyboard , and responding to events and handling events should be implemented in the program code ViewController.swift . So how do I associate the OK button click event with the event handling code in ViewController.swift ? Different computer languages ​​implement it in different ways. In iOS and OS X application development, event processing is realized through the target and action mechanism.



 

 

solution

Controls such as buttons inherit from the UIControl class and have some advanced events. The goal and action mechanism is to associate specific control events with methods in the view controller (or view). This process is called "defining action events". The "target" is the object that responds to the event. In order to facilitate access to other control states, this object is generally a view controller (or view). An "action" is a control's 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 method of the view controller (target), as shown in the following figure, by defining the action event Goals are linked to actions.

 



 

 

There are two ways to realize the connection between the target and the action: Interface Builder connection realization and programming realization.

1. Interface Builder connection implementation

Interface Builder wiring implementation is a storyboard or Xib file, which is realized by wiring.

2. Programming implementation

The programming is realized through the UIControl class addTarget(_:action:forControlEvents:) method. 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
          - labelWidth)/2 , labelTopView, labelWidth, labelHeight))
       
        label.text = "Label"
        //Font left and right in the play
        label.textAlignment = .Center
        self.view.addSubview(label)
       
        let button = UIButton(type: UIButtonType.System)//Create 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
            Ê- buttonWidth)/2 , buttonTopView, buttonWidth, buttonHeight)
       
        button.addTarget(self, action: "onClick:",
            ÊforControlEvents: UIControlEvents.TouchUpInside)
       
        self.view.addSubview(button)  
    }
 
    func onClick(sender: AnyObject) {
        NSLog("OK Button onClick.")
    }   
 
    ...
}

 

}

上述代码中创建并设置UIButton对象,其中创建UIButton对象,参数type是设置按钮的样式,UIButton样式:

  •    Custom。自定义类型。如果不喜欢圆角按钮,可以使用该类型。
  •    System。系统默认属性,表示该按钮没有边框,在iOS 7之前按钮默认为圆角矩形。
  •    Detail Disclosure。细节展示按钮 ,主要用于表视图中的细节展示。
  •    Info LightInfo Dark。这两个是信息按钮 ,样式上与细节展示按钮一样,表示有一些信息需要展示,或有可以设置的内容。
  •    Add Contact。添加联系人按钮

 

代码调用addTarget(_:action:forControlEvents:)方法,方法第一个参数是target,即事件处理对象,本例中是self;方法第二个参数是action,即事件处理对象中的方法,

代码中是"onClick:",方法第三个参数是事件,TouchUpInside事件是按钮的触摸点击事件。

如果调用如下无参数方法:

func onClick() {
}

 

调用代码如下:

button.addTarget(self, action: "onClick",
        ÊforControlEvents: UIControlEvents.TouchUpInside)

 

区别在于action参数"onClick"方法名不同,action参数方法名的冒号暗示了方法名应该具有几个参数。如果要调用的方法是如下3个参数形式:

func onClick(sender: AnyObject, forEvent event: UIEvent) {
}

 

那么调用代码如下:

button.addTarget(self, action: "onClick:forEvent:",
        ÊforControlEvents: UIControlEvents.TouchUpInside)

 

其中"onClick:forEvent:"是调用方法名,onClick表示方法名也是,forEvent表示第二个参数的外部参数名。

 

 

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327058413&siteId=291194637