The function is called from the "iOS Learning note" Objective-c based on the function name

Source: Internet
Author: User

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" >
    1. -(void) CallBack0
    2. {
    3. NSLog (@"CallBack0");
    4. }
    5. -(void) CallBack1: (int) a
    6. {
    7. NSLog (@"callback1:a=%i", a);
    8. }
    9. -(void) CallBack2: (int) a B: (int) b
    10. {
    11. NSLog (@"callback2:a=%i,b=%i", b);
    12. }


Performselector Call:

[CPP]View Plaincopy
    1. [Self performselector: @selector (CallBack0)];
    2. [Self performselector: @selector (CallBack1:) withobject:1];
    3. [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" >
    1. NSString *[email protected]"CallBack0";
    2. SEL faselector=nsselectorfromstring (a);
    3. [Self performselector:faselector];
    4. NSString *[email protected]"CALLBACK1:";
    5. SEL fbselector=nsselectorfromstring (b);
    6. [Self performselector:fbselector withobject:1];
    7. NSString *[email protected]"CALLBACK2:B:";
    8. SEL fcselector=nsselectorfromstring (c);
    9. [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

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.