Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何使用“发送”发送参数#选择器";_Swift_Selector_Swift4 - Fatal编程技术网

Swift 如何使用“发送”发送参数#选择器";

Swift 如何使用“发送”发送参数#选择器";,swift,selector,swift4,Swift,Selector,Swift4,抱歉,我的问题是,我是SWIFT新手,但我想创建计时器,但计时器选择器调用我的ViewController UpdateViewWiwtimer,但此代码返回false,但返回self.test工作 这里有什么问题 问题是目标应该包含要调用的方法,在计时器声明中,您将其设置为self,这样它将在该类中查找方法,您可以尝试吗 class TimerFactory { var second: Int = 0; var timer: Timer = Timer(); init

抱歉,我的问题是,我是SWIFT新手,但我想创建计时器,但计时器选择器调用我的ViewController UpdateViewWiwtimer,但此代码返回false,但返回self.test工作
这里有什么问题

问题是目标应该包含要调用的方法,在计时器声明中,您将其设置为self,这样它将在该类中查找方法,您可以尝试吗

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)
    }
}

您的目标应该是
vc
,因为计时器将在那里查找方法
updateViewTimer

 self.timer = Timer.scheduledTimer(timeInterval: 1, target: vc ,   selector: (#selector(vc.updateViewTimer)), userInfo: nil, repeats: true)
当目标是
self
时,它指的是
TimerFactory


编辑(基于评论):



编辑2(建议):

如果计划在另一个类中重用
TimerFactory
,则需要确保该类中存在
updateViewTimer()
方法,否则代码将崩溃

相反。。。我更愿意建议一种更健壮的方法,使用闭包并放弃对
updateViewTimer(:)
方法的依赖:

@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()
        }
    }        
}


感谢这项工作,但当我想在updateViewTimer方法参数中传递第二个属性时,这是“#selector”的返回参数,它没有引用“@objc”方法、属性或initializer@MuradSofiyev第二个属性是什么?另外,你的
updateViewTimer()
函数现在看起来像什么?(#选择器(vc.updateViewTimer(second:second)))second是我的TimerFactoryproperty@MuradSofiyev首先,
#选择器
接受方法签名。所以这就像是
(#选择器(vc.updateViewTimer(second:)))
一样。@MuradSofiyev我的荣幸:)检查更新的答案以获得更快捷的方法。
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()
        }
    }        
}
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")
        }
    }
}