030_swift_三种消息传递

点击事件 

//
//  ViewController.swift
//  DemoApp
//
//  Created by liuan on 2020/4/23.
//  Copyright © 2020 anguo.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view
       //定义一个整形常量,作为按钮的宽度,该宽度的值为当前屏幕的宽度和40的差
        let width=Int(self.view.frame.size.width-40)
        let greetingButton=UIButton(frame: CGRect(x: 20, y: 100, width: width, height: 40))
        greetingButton.setTitle("Greeting", for: .normal)
        greetingButton.backgroundColor = .orange
        //第一种设置点击事件
//        greetingButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
        greetingButton.tag=1
        let antherMethod:Selector=#selector(buttonTappedFor(_:))
        greetingButton.addTarget(self, action: antherMethod, for: .touchUpInside)
        self.view.addSubview(greetingButton)
        
    }
    @objc func buttonTapped(){
        print("buttonTapped")
    }
    @objc func buttonTappedFor(_ sender:UIButton)

    {
        let tag=sender.tag
            print("Button tag  :\(tag)")
    }

}

消息使用、

//
//  ViewController.swift
//  DemoApp
//
//  Created by liuan on 2020/4/23.
//  Copyright © 2020 anguo.com. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view
        //定义一个整形常量,作为按钮的宽度,该宽度的值为当前屏幕的宽度和40的差
//消息的传递机制。通过定时器
        let timerSelector=#selector(timerAction(_:))
        Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: timerSelector, userInfo: "parameter", repeats: true)
  //选择器在子线程中的使用
        let newSelector=#selector(timerAction2)
        //子线程中调用选择器
        self.perform(newSelector,with: nil,afterDelay: 2.0)
        //主线程调用
        self.perform(newSelector,on:.main,with: nil,waitUntilDone: true)
    }

    @objc func timerAction(_ timer :Timer){
        let parameter=timer.userInfo
        print(parameter ?? "")
    }
    
    @objc func timerAction2(){
  print("timerAction2")
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值