[Swift实际操作]八、实用进阶-(2)Swift语言中的三种消息传递模式

本文将通过响应按钮的点击事件,来演示Target-Action消息传递机制,
该机制主要用于响应用户的界面操作。
打开创建的空白项目。
然后在左侧的项目导航区,打开视图控制器的代码文件:ViewController.swift

第一种消息传递模式:

 1 import UIKit
 2 class ViewController: UIViewController{
 3 
 4   override func viewDidLoad(){
 5     super.viewDidLoad()
 6 //Do any additional setup after loading the view, typically from a nib.
 7 //现在开始编写代码创建一个简单的按钮控件
 8 //定义一个整形常量,作为按钮的宽度,该宽度的值为当前屏幕的宽度和40的差
 9     let wid = Int(self.view.frame,size.width - 40)
10 //然后创建一个指定区域的按钮控件,它的坐标位于(20,100),高度为40,宽度为常量的值
11     let greetingButton = UIButton(frame:CGRect(x: 20,y: 100, wid,height: 40))
12 //设置按钮在正常状态下的标题文字
13     greetingButton.setTitle("Greeting",for: .normal)
14 //设置按钮的背景按钮为橙色
15     greetingButton.backgroundColor = .orange
16 //使用Target-Action方式,给按钮对象绑定点击事件。
17 //响应该事件的方法为ViewController类中的buttonTapped方法,
18 //将在下面代码中完成该方法的编写
19 //如果方法位于当前的类中,可以省略方法名称前方的类名action:#selector(buttonTapped),
20     greetingButton.addTarget(self,
21                            action:#selector(ViewControler.buttonTapped),
22                               for: .touchUpInside)
23 //将按钮对象添加到当前视图控制器的根视图。
24     self.view.addSubview(greetingButton)
25   }
26 
27 //添加一个方法,作为Target-Action中的Action,用来响应按钮的点击事件。
28    @objc func buttonTapped()
29    {
30       print("strengthen")
31     }
32      
33    override func didReceiveMemoryWarning(){
34      super.didReceiveMemoryWarning()
35      //Dispose of any resources that can be recreated.
36 
37    }
38 }

第二种消息传递模式:

 1 import UIKit
 2 class ViewController: UIViewController{
 3 
 4   override func viewDidLoad(){
 5     super.viewDidLoad()
 6 //Do any additional setup after loading the view, typically from a nib.
 7 //现在开始编写代码创建一个简单的按钮控件
 8 //定义一个整形常量,作为按钮的宽度,该宽度的值为当前屏幕的宽度和40的差
 9     let wid = Int(self.view.frame,size.width - 40)
10 //然后创建一个指定区域的按钮控件,它的坐标位于(20,100),高度为40,宽度为常量的值
11     let greetingButton = UIButton(frame:CGRect(x: 20,y: 100, wid,height: 40))
12 //设置按钮在正常状态下的标题文字
13     greetingButton.setTitle("Greeting",for: .normal)
14 //设置按钮的背景按钮为橙色
15     greetingButton.backgroundColor = .orange
16 //设置按钮的标识符为1,通过该标识符剋获得该按钮的控件
17     greetingButton.tag = 1
18 //给按钮绑定点击事件,
19 //注意方法名称的右侧使用了(_:),小括号表示该方法拥有参数,下划线表示隐藏外部参数名。
20     greetingButton.addTarget(self,
21                            action:#selector(buttonTappedFor(_:)),
22                               for: .touchUpInside)
23 //将按钮对象添加到当前视图控制器的根视图。
24     self.view.addSubview(greetingButton)   
25   }
26 
27 //添加一个方法,用来响应按钮的点击事件。
28    @objc func buttonTappedFor(_ sender: UIButton)
29    {
30 //获得按钮的标识值,并在控制台输出。
31       let tag = sender.tag
32       print("button tag: (tag)")
33     }
34      
35    override func didReceiveMemoryWarning(){
36      super.didReceiveMemoryWarning()
37      //Dispose of any resources that can be recreated.
38 
39    }
40 }

第三种消息传递模式:

 1 import UIKit
 2 class ViewController: UIViewController{
 3 
 4   override func viewDidLoad(){
 5     super.viewDidLoad()
 6 //Do any additional setup after loading the view, typically from a nib.
 7 //现在开始编写代码创建一个简单的按钮控件
 8 //定义一个整形常量,作为按钮的宽度,该宽度的值为当前屏幕的宽度和40的差
 9     let wid = Int(self.view.frame,size.width - 40)
10 //然后创建一个指定区域的按钮控件,它的坐标位于(20,100),高度为40,宽度为常量的值
11     let greetingButton = UIButton(frame:CGRect(x: 20,y: 100, wid,height: 40))
12 //设置按钮在正常状态下的标题文字
13     greetingButton.setTitle("Greeting",for: .normal)
14 //设置按钮的背景按钮为橙色
15     greetingButton.backgroundColor = .orange
16 //初始化一个选择器对象,并设置它的方法名称。
17     let anotherMethod: Selector = #selector(buttonTappedFor(_:))
18 //然后将该选择器对象,添加到按钮的目标之中,
19 //使用该选择器中的方法,响应按钮的点击事件
20     greetingButton.addTarget(self,
21                            action:anotherMethod,
22                               for: .touchUpInside)
23 //将按钮对象添加到当前视图控制器的根视图。
24     self.view.addSubview(greetingButton)
25   }
26 
27 //添加一个方法,用来响应按钮的点击事件。
28    @objc func buttonTappedFor(_ sender: UIButton)
29    {
30 //获得按钮的标识值,并在控制台输出。
31       let tag = sender.tag
32       print("button tag: (tag)")
33     }
34      
35    override func didReceiveMemoryWarning(){
36      super.didReceiveMemoryWarning()
37      //Dispose of any resources that can be recreated.
38 
39    }
40 }

选择器和定时器搭配使用

 1 import UIKit
 2 class ViewController: UIViewController{
 3 
 4   override func viewDidLoad(){
 5     super.viewDidLoad()
 6 //Do any additional setup after loading the view, typically from a nib.
 7 //现在开始编写代码创建一个简单的按钮控件
 8 //定义一个整形常量,作为按钮的宽度,该宽度的值为当前屏幕的宽度和40的差
 9     let wid = Int(self.view.frame,size.width - 40)
10 //然后创建一个指定区域的按钮控件,它的坐标位于(20,100),高度为40,宽度为常量的值
11     let greetingButton = UIButton(frame:CGRect(x: 20,y: 100, wid,height: 40))
12 //设置按钮在正常状态下的标题文字
13     greetingButton.setTitle("Greeting",for: .normal)
14 //设置按钮的背景按钮为橙色
15     greetingButton.backgroundColor = .orange
16 //定义一个新的选择器
17     let newSelector = #selector(calledByController)
18 
19 //方式(1)通过视图控制器对象的perform方法,在子线程中调用该选择器 
20     self.perform(newSelector)
21 
22 //方式(2)在子线程中调用选择器,传递的参数值为空,并延迟两秒执行。
23     self.perform(newSelector,with: nil, afterDelay: 2.0)
24 
25 //方式(3)在主线程中调用选择你器,并等待动作的执行。
26 //当对界面元素进行操作时,需要在主线程中进行
27     self.perform(newSelector,
28                    on: .main,
29                  with: nil,
30         waitUntilDone: true)
31 
32 //方式(4)最后选择在后台线程中运行整个方法,这样就无需关心后台线程中的方法的执行
33     self.performSelector(inBackground: newSelector,with: nil)  
34   }
35 
36 //实现选择器中的方法
37    @objc func calledByController()
38    {
39       print("strengthen")
40     }
41      
42    override func didReceiveMemoryWarning(){
43      super.didReceiveMemoryWarning()
44      //Dispose of any resources that can be recreated.
45 
46    }
47 }
原文地址:https://www.cnblogs.com/strengthen/p/9806867.html