0

hey i am trying to call a function every 1 second but i keep getting the following error:

terminating with uncaught exception of type NSException

this is the code i have and i get the error when i press the button.

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)

i have no clue how to fix this error :( any help would be greatly appreciated!!

luk2302
  • 55,258
  • 23
  • 97
  • 137
FutureCake
  • 2,614
  • 3
  • 27
  • 70
  • 1
    Is `countDownFunc()` and `func countDownFunc2()` nested in another method? If yes, put it out. The target/action methods must be located on the top level of the class. – vadian Dec 25 '15 at 10:48
  • Please post the exact error message and the entire function you are currently operating in - I am guessing what @vadian just mentioned is true and I already wrote an answer regarding that. But that can only be verified if you provide a little bit more code context. – luk2302 Dec 25 '15 at 10:53

2 Answers2

1

It looks like you defined the method in the wrong scope. You seem to have something like

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)

}

That means that the functions countDownFunc and countDownFunc2 are only defined and available in the scope of mySpecialFunc. The object/class you are operating on does not know anything about it and therefore your timer and action selector both fail. What you have to do is move the two methods out of the body of 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"
}
luk2302
  • 55,258
  • 23
  • 97
  • 137
0

Hey look at this.

myButton.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)
let timer = NSTimer.scheduledTimerWithTimeInterval(timeInterval: 1, target: self, selector: "test", userInfo: nil, repeats: false)
// cit

He didn't use selector: Selector("method name"), he use selector: "method name"

Community
  • 1
  • 1
  • 1
    That doesn't matter. A `Selector` type can be represented by a literal string, because it conforms to the `StringLiteralConvertible` protocol. – vadian Dec 25 '15 at 11:16