【问题标题】:swift NSTimer userinfoswift NSTimer 用户信息
【发布时间】:2015-05-22 07:24:59
【问题描述】:

我正在尝试使用 NSTimer 的用户信息传递 UIButton。我已经阅读了 NSTimers 上关于 stackoverflow 的每一篇文章。我已经非常接近了,但无法完全到达那里。这篇文章有帮助

Swift NSTimer retrieving userInfo as CGPoint

func timeToRun(ButonToEnable:UIButton) {
    var  tempButton = ButonToEnable
    timer = NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("setRotateToFalse"), userInfo: ["theButton" :tempButton], repeats: false)    
}

定时器运行的函数

func setRotateToFalse() {
    println(  timer.userInfo )// just see whats happening

    rotate = false
    let userInfo = timer.userInfo as Dictionary<String, AnyObject>
    var tempbutton:UIButton = (userInfo["theButton"] as UIButton)
    tempbutton.enabled = true
    timer.invalidate()    
}

【问题讨论】:

    标签: swift nstimer userinfo


    【解决方案1】:

    我知道你已经设法解决了这个问题,但我想我会给你一些关于使用NSTimer 的更多信息。访问计时器对象和用户信息的正确方法是使用它,如下所示。初始化计时器时,您可以这样创建它:

    斯威夫特 2.x

    NSTimer.scheduledTimerWithTimeInterval(4, target: self, selector: Selector("setRotateToFalse:"), userInfo: ["theButton" :tempButton], repeats: false)
    

    斯威夫特 3.x
    Timer.scheduledTimer(timeInterval: 1, target: self, selector:#selector(ViewController.setRotateToFalse), userInfo: ["theButton" :tempButton], repeats: false)
    

    那么回调是这样的:

    func setRotateToFalse(timer:NSTimer) {
        rotate = false
        let userInfo = timer.userInfo as Dictionary<String, AnyObject>
        var tempbutton:UIButton = (userInfo["theButton"] as UIButton)
        tempbutton.enabled = true
        timer.invalidate()    
    }
    

    因此,您无需保留对计时器的引用,并尽可能避免经常使用讨厌的全局变量。如果您的类没有从 NSObject 继承,您可能会在 swift 中遇到问题,它说没有定义回调,但可以通过在函数定义的开头添加 @objc 轻松解决此问题。

    【讨论】:

    • 没问题。有空的时候能不能给个答案。干杯
    • 此外,您不必将字典作为userInfo 传递,您只需要一个符合AnyObject? 的对象。就个人而言,我用它传递了一个String
    【解决方案2】:

    我正要发布这个,因为我在发布之前阅读了它。我注意到我在 userinfo 之前有timer.invalidate(),所以这就是它不起作用的原因。我会发布它,因为它可能对其他人有帮助。

    func setRotateToFalse(timer:NSTimer) {
        rotate = false
        timer.invalidate()
        let userInfo = timer.userInfo as Dictionary<String, AnyObject>
        var tempbutton:UIButton = (userInfo["theButton"] as UIButton)
        tempbutton.enabled = true
    }
    

    【讨论】:

      【解决方案3】:

      macOS 10.12+ 和 iOS 10.0+ 引入了基于块的 API Timer,这是一种更方便的方式

      func timeToRun(buttonToEnable: UIButton) {
          timer = Timer.scheduledTimer(withTimeInterval:4, repeats: false) { timer in 
              buttonToEnable.enabled = true
          }    
      }
      

      单发计时器在触发后将自动失效。


      使用 GCD (DispatchQueue.main.asyncAfter)

      func timeToRun(buttonToEnable: UIButton) {
          DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(4)) {
              buttonToEnable.enabled = true
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-07-21
        • 1970-01-01
        • 1970-01-01
        • 2012-03-20
        • 2014-09-09
        • 1970-01-01
        • 2014-08-13
        • 2016-04-17
        • 1970-01-01
        相关资源
        最近更新 更多