@selector() in Swift?

后端 未结 23 2439
清酒与你
清酒与你 2020-11-21 15:24

I\'m trying to create an NSTimer in Swift but I\'m having some trouble.

NSTimer(timeInterval: 1, target: self, selector: test(), us         


        
相关标签:
23条回答
  • 2020-11-21 16:15

    Also, if your (Swift) class does not descend from an Objective-C class, then you must have a colon at the end of the target method name string and you must use the @objc property with your target method e.g.

    var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
    
    @objc func method() {
        // Something cool here   
    } 
    

    otherwise you will get a "Unrecognised Selector" error at runtime.

    0 讨论(0)
  • 2020-11-21 16:15

    Swift 4.0

    you create the Selector like below.

    1.add the event to a button like:

    button.addTarget(self, action: #selector(clickedButton(sender:)), for: UIControlEvents.touchUpInside)
    

    and the function will be like below:

    @objc func clickedButton(sender: AnyObject) {
    
    }
    
    0 讨论(0)
  • 2020-11-21 16:15

    you create the Selector like below.
    1.

    UIBarButtonItem(
        title: "Some Title",
        style: UIBarButtonItemStyle.Done,
        target: self,
        action: "flatButtonPressed"
    )
    

    2.

    flatButton.addTarget(self, action: "flatButtonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
    

    Take note that the @selector syntax is gone and replaced with a simple String naming the method to call. There’s one area where we can all agree the verbosity got in the way. Of course, if we declared that there is a target method called flatButtonPressed: we better write one:

    func flatButtonPressed(sender: AnyObject) {
      NSLog("flatButtonPressed")
    }
    

    set the timer:

        var timer = NSTimer.scheduledTimerWithTimeInterval(1.0, 
                        target: self, 
                        selector: Selector("flatButtonPressed"), 
                        userInfo: userInfo, 
                        repeats: true)
        let mainLoop = NSRunLoop.mainRunLoop()  //1
        mainLoop.addTimer(timer, forMode: NSDefaultRunLoopMode) //2 this two line is optinal
    

    In order to be complete, here’s the flatButtonPressed

    func flatButtonPressed(timer: NSTimer) {
    }
    
    0 讨论(0)
  • 2020-11-21 16:17

    Just in case somebody else have the same problem I had with NSTimer where none of the other answers fixed the issue, is really important to mention that, if you are using a class that do not inherits from NSObject either directly or deep in the hierarchy(e.g. manually created swift files), none of the other answers will work even when is specified as follows:

    let timer = NSTimer(timeInterval: 1, target: self, selector: "test", 
                        userInfo: nil, repeats: false)
    func test () {}
    

    Without changing anything else other than just making the class inherit from NSObject I stopped getting the "Unrecognized selector" Error and got my logic working as expected.

    0 讨论(0)
  • 2020-11-21 16:18

    If you want to pass a parameter to the function from the NSTimer then here is your solution:

    var somethingToPass = "It worked"
    
    let timer = NSTimer.scheduledTimerWithTimeInterval(0.01, target: self, selector: "tester:", userInfo: somethingToPass, repeats: false)
    
    func tester(timer: NSTimer)
    {
        let theStringToPrint = timer.userInfo as String
        println(theStringToPrint)
    }
    

    Include the colon in the selector text (tester:), and your parameter(s) go in userInfo.

    Your function should take NSTimer as a parameter. Then just extract userInfo to get the parameter that passed.

    0 讨论(0)
提交回复
热议问题