Source: http://blog.csdn.net/hopedark/article/details/7970370
The method of calling a function in Objective-c is "message passing", and the difference between this and a normal function call is that you can pass any message to an object at any time without having to declare the methods at compile time. So objective-c can pass the message at runtime.
The main use:sel and @selector, and of course Performselector
See Example:
First three functions:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- -(void) CallBack0
- {
- NSLog (@"CallBack0");
- }
- -(void) CallBack1: (int) a
- {
- NSLog (@"callback1:a=%i", a);
- }
- -(void) CallBack2: (int) a B: (int) b
- {
- NSLog (@"callback2:a=%i,b=%i", b);
- }
Performselector Call:
[CPP]View Plaincopy
- [Self performselector: @selector (CallBack0)];
- [Self performselector: @selector (CallBack1:) withobject:1];
- [Self performselector: @selector (callback2:b:) withobject:1 withobject:2];
Dynamic invocation According to the name of the function:
[CPP]View Plaincopy < param name= "wmode" value= "Transparent" >
- NSString *[email protected]"CallBack0";
- SEL faselector=nsselectorfromstring (a);
- [Self performselector:faselector];
- NSString *[email protected]"CALLBACK1:";
- SEL fbselector=nsselectorfromstring (b);
- [Self performselector:fbselector withobject:1];
- NSString *[email protected]"CALLBACK2:B:";
- SEL fcselector=nsselectorfromstring (c);
- [Self performselector:fcselector withobject:1 withobject:2];
This allows the function to be called according to the function name!
iOS Learning notes objective-c calling functions based on function name