当我调用内部带有 NSTimer 的函数时出现 NSException 错误
NSException errror when i call a function with a NSTimer inside
嘿,我试图每 1 秒调用一个函数,但我不断收到以下错误:
terminating with uncaught exception of type NSException
这是我的代码,当我按下按钮时出现错误。
var startButton : UIButton!
var theTime = 0;
var countDownText = "hello"
var countDownTimer = NSTimer
startButton = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
startButton.center = CGPointMake(view.frame.size.width / 2, view.frame.size.height/3)
startButton.setTitle("\(countDownText)", forState: UIControlState.Normal)
startButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
startButton.addTarget(self, action: Selector("countDownFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(startButton)
func countDownFunc() {
countDownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDownFunc2:"), userInfo: nil, repeats: true)
}
func countDownFunc2(){
theTime++
countDownText = "HELLOOOOOOOOO"
}
print(theTime)
我不知道如何解决这个错误:(
任何帮助将不胜感激!!
您似乎在错误的范围内定义了方法。你好像有点像
func mySpecialFunc() {
...
startButton.addTarget(self, action: Selector("countDownFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(startButton)
func countDownFunc() {
countDownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDownFunc2:"), userInfo: nil, repeats: true)
}
func countDownFunc2(){
theTime++
countDownText = "HELLOOOOOOOOO"
}
print(theTime)
}
这意味着函数countDownFunc
和countDownFunc2
仅在mySpecialFunc
的范围内定义和可用。您正在操作的 object/class 对此一无所知,因此您的计时器和动作选择器都失败了。您需要做的是将这两个方法移出 mySpecialFunc
:
的主体
func mySpecialFunc() {
...
startButton.addTarget(self, action: Selector("countDownFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(startButton)
}
func countDownFunc() {
countDownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDownFunc2"), userInfo: nil, repeats: true)
}
func countDownFunc2(){
theTime++
countDownText = "HELLOOOOOOOOO"
}
嘿看看this。
myButton.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)
let timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval: 1, target: self, selector: "test", userInfo: nil, repeats: false)
// cit
他没用selector: Selector("method name")
,他用selector: "method name"
嘿,我试图每 1 秒调用一个函数,但我不断收到以下错误:
terminating with uncaught exception of type NSException
这是我的代码,当我按下按钮时出现错误。
var startButton : UIButton!
var theTime = 0;
var countDownText = "hello"
var countDownTimer = NSTimer
startButton = UIButton(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: view.frame.size.height))
startButton.center = CGPointMake(view.frame.size.width / 2, view.frame.size.height/3)
startButton.setTitle("\(countDownText)", forState: UIControlState.Normal)
startButton.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
startButton.addTarget(self, action: Selector("countDownFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(startButton)
func countDownFunc() {
countDownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDownFunc2:"), userInfo: nil, repeats: true)
}
func countDownFunc2(){
theTime++
countDownText = "HELLOOOOOOOOO"
}
print(theTime)
我不知道如何解决这个错误:( 任何帮助将不胜感激!!
您似乎在错误的范围内定义了方法。你好像有点像
func mySpecialFunc() {
...
startButton.addTarget(self, action: Selector("countDownFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(startButton)
func countDownFunc() {
countDownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDownFunc2:"), userInfo: nil, repeats: true)
}
func countDownFunc2(){
theTime++
countDownText = "HELLOOOOOOOOO"
}
print(theTime)
}
这意味着函数countDownFunc
和countDownFunc2
仅在mySpecialFunc
的范围内定义和可用。您正在操作的 object/class 对此一无所知,因此您的计时器和动作选择器都失败了。您需要做的是将这两个方法移出 mySpecialFunc
:
func mySpecialFunc() {
...
startButton.addTarget(self, action: Selector("countDownFunc"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(startButton)
}
func countDownFunc() {
countDownTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("countDownFunc2"), userInfo: nil, repeats: true)
}
func countDownFunc2(){
theTime++
countDownText = "HELLOOOOOOOOO"
}
嘿看看this。
myButton.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)
let timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval: 1, target: self, selector: "test", userInfo: nil, repeats: false)
// cit
他没用selector: Selector("method name")
,他用selector: "method name"