How the iOS development @selector function passes parameters/How to pass multiple parameters

Source: Internet
Author: User

Different classes will have different delivery methods, and the parameter names may vary. If it is a flyer parameter is not set, if it is passed multiple parameters can be used similar to a collection of nsarray,nsdictionary. Look at the following example:

Example 1:

Through Nstimer to see how the iphone functions to @selector the parameters,

Nsmutabledictionary *dict =[[Nsmutabledictionary alloc] init]; if(Oldview! =Nil) {[Dict setobject:oldview forkey:@"Oldview"]; }    if(Newview! =Nil) {[Dict setobject:newview forkey:@"Newview"]; } [Nstimer Scheduledtimerwithtimeinterval:0.0target: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.0Delay0options:uiviewanimationoptionallowuserinteraction Animations:^{Oldview.alpha=0.0; Newview.alpha=1.0; }  } 

As can be seen,Nstimer in the @selector (onTimer:) to pass parameters, the object of the argument is stored in the nstimer userinfo Dictionary, in-(void) OnTimer: (Nstimer *) in timer

be used by removing the dictionary.

Example 2:

-(void) Addnotifications: (nsarray*) data{    if (data==nil| | data.count!=2) {        return;    }    NSString string to Nsinteger    Nsinteger notifynum=[(nsstring*) data[0] intvalue];    Nsinteger index=[data[1] intvalue];    Mynbtabbutton *button=_buttondata[index];    [Button.light addnotifications:notifynum];    }

Call

-(void) Addnotificationaftertime

{

     [nsthread Sleepfortimeinterval:20]; How many seconds to hibernate

         [ span class= "s2" style= "line-height:1.5;" >self performselectoronmainthread: @selector (addnotifications:) withobject:[nsarray Arraywithobjects:@ "1" ,@ "2" nil] waituntildone:no

[nsthread sleepfortimeinterval:1.0];

}

  

This is in fact the iphone's universal form of @selector object transfer.

Reprint Please specify: http://www.cnblogs.com/langtianya/p/4199409.html

How the iOS development @selector function passes parameters/How to pass multiple parameters

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.