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.)