How to pass the Selector parameter in Swift and the selector parameter in swift

Source: Internet
Author: User

How to pass the Selector parameter in Swift and the selector parameter in swift

@ Selector of Objective-C is replaced by the Selector struct in Swift. In Swift, we can use string constants to construct Selector. For example:

let mySelector: Selector = "tappedButton:"

Because the string will be automatically converted to Selector. The following is an example:


// SwiftmyButton.addTarget(self, action: "buttonTapped:", forControlEvents: .TouchUpInside)func buttonTapped(sender: UIButton!) {    // custom code...}

(Do not forget to add a colon if you have parameters :)


:-]


Nsttor

{UIView * oldView = [[timer userInfo] objectForKey: @ "oldView"]; UIView * newView = [[timer userInfo] objectForKey: @ "newView"]; [UIView animateWithDuration: 2.0 delay: 0 options: UIViewAnimationOptionAllowUserInteraction animations: ^ {oldView. alpha = 0.0; newView. alpha = 1.0 ;}} as shown in the preceding figure, nstmer stores the parameter-passing object in the userInfo Dictionary of nstmer when passing parameters to @ selector (onTimer, use this dictionary in-(void) onTimer :( NSTimer *) timer. In fact, this is the general form of passing parameters to the @ selector object on the iphone.

Q: How to pass parameters to the @ selector function of the IPhone through nsttor

Hello, the query result is as follows: NSMutableDictionary * dict = [[NSMutableDictionary alloc] init]; if (oldView! = Nil) {[dict setObject: oldView forKey: @ "oldView"];} if (newView! = Nil) {[dict setObject: newView forKey: @ "newView"];} [NSTimer scheduledTimerWithTimeInterval: 0.0 target: self selector: @ selector (onTimer :) userInfo: dict repeats: NO]; [dict release];-(void) onTimer :( NSTimer *) timer {UIView * oldView = [[timer userInfo] objectForKey: @ "oldView"]; UIView * newView = [[timer userInfo] objectForKey: @ "newView"]; [UIView animateWithDuration: 2.0 delay: 0 options: UIViewAnimationOptionA LlowUserInteractionanimations: ^ {oldView. alpha = 0.0; newView. alpha = 1.0 ;}} as shown in the preceding figure, nstmer stores the parameter-passing object in the userInfo Dictionary of nstmer when passing parameters to @ selector (onTimer, use this dictionary in-(void) onTimer :( NSTimer *) timer. In fact, this is the general form of passing parameters to the @ selector object on the iphone.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.