Summary of the use of Performselector

Source: Internet
Author: User
In a project, you often use methods such as PERFORMSELECTORXXX, which are collected here:

1.performselectoronmainthread:withobject:waituntildone:

-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait


Executes the specified method on the main thread, using the default mode (Nsdefaultrunloopmode).

The default mode means that the method in the main thread is queued, is a loop queue, and is executed in a loop.

Parameters:

Aselector: The method to execute on the main thread cannot have a return value and can have only one argument.


ARG: The argument to pass, if there is no argument, set to nil


Wait: The Aselector method to execute is immediately executed.

If set to Yes: The main thread executes the Aselector method after waiting for the current thread to finish executing;

Set to No: Executes the Aselector method on the main thread without waiting for the current thread to finish executing.

If the current thread is the primary, then the Aselector method executes immediately.


The method uses: Because the iphone is programmed, UI modifications can only be performed on the main thread. You can use this method to complete the modification of the UI.



2.performselector:withobject:afterdelay:

-(void) Performselector: (SEL) Aselector withobject: (ID) anargument afterdelay: (nstimeinterval) delay


Executes the specified method in the current thread, uses the default mode, and specifies the delay.

Parameters:

Aselector: The specified method. The meaning of the same, not to repeat.

Anargument: Ditto

Delay: Specifies the delay time (in seconds).


3.performSelector


We often use the following 3 methods, respectively:


-(ID) Performselector: (SEL) Aselector;

-(ID) Performselector: (SEL) Aselector withobject: (ID) object;

-(ID) Performselector: (SEL) Aselector withobject: (ID) object1 withobject: (ID) object2;


First, define the method to invoke


-(void) methodnoparam{

NSLog (@ "Methodnoparam");

}


-(void) Methodwithoneparam: (ID) paramfirst{

NSLog (@ "Methodwithoneparam:%@", Paramfirst);

}


-(void) Methodwithparams: (ID) Paramfirst Andparamsecond: (ID) paramsecond{

NSLog (@ "Methodwithoneparam:%@,%@", Paramfirst,paramsecond);

}
Second, make a call to

No parameters

BOOL isnoparam= [self.selfviewcontrollerdelegaterespondstoselector: @selector (Methodnoparam)];

if (Isnoparam) {

[Self.selfviewcontrollerdelegate performselector: @selector (Methodnoparam)];

}

A parameter

BOOL isoneparam= [self.selfviewcontrollerdelegaterespondstoselector: @selector (methodwithoneparam:)];

if (Isoneparam) {

[Self.selfviewcontrollerdelegate performselector: @selector (methodwithoneparam:) withobject:@ "Firsht"];

}

Two parameters

BOOL isparams= [self.selfviewcontrollerdelegaterespondstoselector: @selector (methodwithparams:andparamsecond:)];

if (isparams) {

[Self.selfviewcontrollerdelegate performselector: @selector (methodwithparams:andparamsecond:) withObject:@ "a" withobject:@ "Second"];

}

Supplementary Note:

The Run loop is equivalent to the message loop mechanism in Win32, which allows you to schedule events/messages (mouse, keyboard, touch, timer messages, etc.)

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.