@selector() in Swift?

后端 未结 23 2537
清酒与你
清酒与你 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 15:53

    When using performSelector()

    /addtarget()/NStimer.scheduledTimerWithInterval() methods your method (matching the selector) should be marked as

    @objc
    For Swift 2.0:
        {  
            //...
            self.performSelector(“performMethod”, withObject: nil , afterDelay: 0.5)
            //...
    
    
        //...
        btnHome.addTarget(self, action: “buttonPressed:", forControlEvents: UIControlEvents.TouchUpInside)
        //...
    
        //...
         NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector : “timerMethod”, userInfo: nil, repeats: false)
        //...
    
    }
    
    @objc private func performMethod() {
    …
    }
    @objc private func buttonPressed(sender:UIButton){
    ….
    }
    @objc private func timerMethod () {
    ….
    }
    

    For Swift 2.2, you need to write '#selector()' instead of string and selector name so the possibilities of spelling error and crash due to that will not be there anymore. Below is example

    self.performSelector(#selector(MyClass.performMethod), withObject: nil , afterDelay: 0.5)
    
    0 讨论(0)
  • 2020-11-21 16:01

    For future readers, I found that I experienced a problem and was getting an unrecognised selector sent to instance error that was caused by marking the target func as private.

    The func MUST be publicly visible to be called by an object with a reference to a selector.

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

    selector is a word from Objective-C world and you are able to use it from Swift to have a possibility to call Objective-C from Swift It allows you to execute some code at runtime

    Before Swift 2.2 the syntax is:

    Selector("foo:")
    

    Since a function name is passed into Selector as a String parameter("foo") it is not possible to check a name in compile time. As a result you can get a runtime error:

    unrecognized selector sent to instance
    

    After Swift 2.2+ the syntax is:

    #selector(foo(_:))
    

    Xcode's autocomplete help you to call a right method

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

    Selectors are an internal representation of a method name in Objective-C. In Objective-C "@selector(methodName)" would convert a source-code method into a data type of SEL. Since you can't use the @selector syntax in Swift (rickster is on point there), you have to manually specify the method name as a String object directly, or by passing a String object to the Selector type. Here is an example:

    var rightBarButton = UIBarButtonItem(
        title: "Logout", 
        style: UIBarButtonItemStyle.Plain, 
        target: self, 
        action:"logout"
    )
    

    or

    var rightBarButton = UIBarButtonItem(
        title: "Logout", 
        style: UIBarButtonItemStyle.Plain, 
        target: self, 
        action:Selector("logout")
    )
    
    0 讨论(0)
  • 2020-11-21 16:03

    Here's a quick example on how to use the Selector class on Swift:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        var rightButton = UIBarButtonItem(title: "Title", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("method"))
        self.navigationItem.rightBarButtonItem = rightButton
    }
    
    func method() {
        // Something cool here   
    }
    

    Note that if the method passed as a string doesn't work, it will fail at runtime, not compile time, and crash your app. Be careful

    0 讨论(0)
  • 2020-11-21 16:03
    Create Refresh control using Selector method.   
        var refreshCntrl : UIRefreshControl!
        refreshCntrl = UIRefreshControl()
        refreshCntrl.tintColor = UIColor.whiteColor()
        refreshCntrl.attributedTitle = NSAttributedString(string: "Please Wait...")
        refreshCntrl.addTarget(self, action:"refreshControlValueChanged", forControlEvents: UIControlEvents.ValueChanged)
        atableView.addSubview(refreshCntrl)
    

    //Refresh Control Method

    func refreshControlValueChanged(){
        atableView.reloadData()
        refreshCntrl.endRefreshing()
    
    }
    
    0 讨论(0)
提交回复
热议问题