0

Im creating a simple typing app with timer. The timer should be in a form of a progress bar that decrements every second.

To implement the progressbar, I intended to set the progress to 0.1 lesser to the current progress every 1 second. But there is an "Unrecognized selector for instance " error when i set the progress. Is there any other way to work around.

import Foundation
import UIKit

class TestView: UIViewController, UITextInputTraits {


    @IBOutlet weak var TestLabel: UILabel!
    @IBOutlet weak var TypingField: UITextField!
    @IBOutlet weak var progressView: UIProgressView!

    var time : Float = 0.0
    var timer: NSTimer!
    var test = 0;
    var progress : Float = 1
    var myMutableString = NSMutableAttributedString()
    var testStringArray = ["abode" , "tutorial" , "completed",
                            "war", "method", "continue",
                            "machine", "texting" , "iterate"]
    var idx = 0;    
    var setProg : Float = 1     

    func textFieldDidChange(textField: UITextField) {

        let s = TypingField.text!

        if(s.characters.last == " "){
            let word = s.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

            if(!word.isEmpty){
                print(testStringArray[idx])
                if(word == testStringArray[idx]){
                    idx++;
                    TypingField.text = "";
                    TestLabel.text = testStringArray[idx];
                }else{
                    TypingField.text = "";
                }
            }
        }
    }

    func setProgress() {
        setProg -= 0.1
        progressView.progress = setProg  <-- cannot decrement progress bar

    }



    override func viewDidLoad() {
        super.viewDidLoad() 

        TypingField.autocorrectionType = .No
        TypingField.autocapitalizationType = .None 

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true)  

        TestLabel.text = testStringArray[idx];

        TypingField.addTarget(self, action: "textFieldDidChange:", forControlEvents: UIControlEvents.EditingChanged)                            
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
XDProgrammer
  • 853
  • 2
  • 14
  • 31
  • Good chance it is this line causing the problem... `timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:Selector("setProgress"), userInfo: nil, repeats: true) ` try changing to `timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector:"setProgress", userInfo: nil, repeats: true)` – MikeG Feb 08 '16 at 03:10
  • Let me know if that does not work and I'll look more closely – MikeG Feb 08 '16 at 03:11
  • I second @MikeG: http://stackoverflow.com/questions/24007650/selector-in-swift?rq=1 – Louis Tur Feb 08 '16 at 03:13
  • Hi, I tried the solution but still got same error `2016-02-07 19:14:59.474 Typing Test[5611:157671] -[UIView setProgress:]: unrecognized selector sent to instance 0x7fe079c591c0 2016-02-07 19:14:59.485 Typing Test[5611:157671] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setProgress:]: unrecognized selector sent to instance 0x7fe079c591c0'` – XDProgrammer Feb 08 '16 at 03:16

3 Answers3

0

You have coded scheduledTimerWithTimeInterval in a manner like it is done with Objective-C. In Swift it is done differently. Instead of:

    selector:Selector("setProgress")

just use:

    selector:"setProgress"
Michael
  • 8,891
  • 3
  • 29
  • 42
  • Hi, I stil got the same error... but when i uncomment `progressView.progress = setProg` the program will just run fine.. maybe setting the progress create the error? – XDProgrammer Feb 08 '16 at 03:19
  • 1
    Have you connected your outlets properly? That is the only other reason I can think for the error "unrecognized selector sent to instance..." – MikeG Feb 08 '16 at 03:23
  • @MikeG I had checked again,, i think i have.. properly connected it,, – XDProgrammer Feb 08 '16 at 03:31
  • It sounds like some size-effect is causing that line to fail. Could the progress be going negative? I thought it coped with that though. – Michael Feb 08 '16 at 03:39
0

Change the function declaration for func setProgress() to:

func setProgress(sender: NSTimer)

And change Selector("setProgress") to "setProgress:"

EDIT:

Apparently the method setProgress already exists, so you'll need to rename the function, e.g. to setProgressBar or something to that effect.

Method 'setProgress' with Objective-C selector 'setProgress:' conflicts with setter for 'progress' with the same Objective-C selector

xoudini
  • 7,001
  • 5
  • 23
  • 37
  • Wouldn't it be `"setProgress"` ? I have only used this syntax -> `"setProgress:"` when dealing with `NSNotificationCenter` – MikeG Feb 08 '16 at 03:22
  • 1
    Once you add a sender to (or other parameter) to the function, you need to add the colon at the end of the selector. But apparently `setProgress:` conflicts with another setter, so you should name your function something else. I'm getting: `Method 'setProgress' with Objective-C selector 'setProgress:' conflicts with setter for 'progress' with the same Objective-C selector` – xoudini Feb 08 '16 at 03:26
  • I still the same error, I added parameter sender. I changed the function name because there's a conflict,, but the error is still there.. i think its when I set the progress `progressView.progress = setProg` . Error will be gone if i comment this line.. – XDProgrammer Feb 08 '16 at 03:31
  • Comment that line out and try printing out `setProg` inside the `setProgress` function, and if it does decrement the problem isn't with the method nor selector. – xoudini Feb 08 '16 at 03:34
  • yes it does decrement.. `0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2` – XDProgrammer Feb 08 '16 at 03:37
  • The `progressView.progress = setProg` seems the error, when i try setting it in viewDidLoad , it creates the same error.. – XDProgrammer Feb 08 '16 at 03:40
-1

I think the error was fix when I delete and recreate the progressbar.. it may have been as MikeG suggested about incorrect connection on the outlet. Anyways thanks all for your help..

XDProgrammer
  • 853
  • 2
  • 14
  • 31