如何使用"#selector"发送参数?

时间:2018-03-22 18:44:07

标签: swift selector swift4

class TimerFactory {
    var second: Int = 0;
    var timer: Timer = Timer();

    init(second: Int) {
        if(second > 0) {
            self.second = second;
        }
    }

    func runTimer(_ vc: ViewController) -> Void {

        self.timer = Timer.scheduledTimer(timeInterval: 1, target: vc,   selector: (#selector(vc.updateViewTimer)), userInfo: nil, repeats: true)
    }
}


class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @objc func updateViewTimer() {
        print("Hello")
    }
}

extension ViewController {
    @IBAction func startCounter(_ sender: UIBarButtonItem) {
        let timer = TimerFactory(second: 60);
        timer.runTimer(self)
    }
}

Sory我的问题我是SWIFT的新手但是我想创建计时器但是计时器选择器调用我的ViewController updateViewTimer但这段代码返回false但是self.test工作 这里有什么问题?

2 个答案:

答案 0 :(得分:1)

您的目标应为vc,因为计时器将查找方法updateViewTimer

func runTimer(_ vc: ViewController) -> Void {
    self.timer = Timer.scheduledTimer(timeInterval: 1,
                                      target: vc,
                                      selector: (#selector(vc.updateViewTimer)),
                                      userInfo: nil,
                                      repeats: true)
}

当目标为self时,它会引用TimerFactory

编辑 (基于评论)

func runTimer(_ vc: ViewController) -> Void {
    self.timer = Timer.scheduledTimer(timeInterval: 1,
                                      target: vc,
                                      selector: (#selector(vc.updateViewTimer(timer:))),
                                      userInfo: self,
                                      repeats: true)
}
@objc func updateViewTimer(_ timer: Timer) {
    if let factory = timer.userInfo as? TimerFactory {
        factory.second -= 1

        print(factory.second)

        if factory.second == 0 {
            print("completed")
            timer.invalidate()
        }
    }        
}

编辑2 (建议)

如果您打算在另一个课程中重复使用TimerFactory,那么您需要确保此课程中存在updateViewTimer()方法,否则代码将崩溃。

相反......我宁愿建议一种更强大的方法,它将使用闭包并放弃对updateViewTimer(_:)方法的依赖:

func runTimer(_ vc: ViewController, updateHandler: @escaping (_ second: Int)->Void) -> Void {
    self.timer = Timer.scheduledTimer(withTimeInterval: 1,
                                      repeats: true,
                                      block: { (timer) in
                                        self.second -= 1
                                        updateHandler(self.second)

                                        if self.second == 0 {
                                            timer.invalidate()
                                        }
    })
}
func startCounter() {
    let timer = TimerFactory(second: 10);
    timer.runTimer(self) { (second) in
        print(second)

        if second == 0 {
            print("completed")
        }
    }
}

答案 1 :(得分:0)

问题是目标应该包含要调用的方法,在计时器声明中你将它设置为self,所以它会在该类中查找一个方法,你可以试试吗

 self.timer = Timer.scheduledTimer(timeInterval: 1, target: vc ,   selector: (#selector(vc.updateViewTimer)), userInfo: nil, repeats: true)