"COCOS2D-JS Official Document" 23, how to use JS Direct call OC method on the iOS platform

Source: Internet
Author: User

In the Cocos2d-js v3.0 RC2, like on Android JS call Java, COCOS2D-JS also provides a way to call Objective-c on iOS and Mac directly, the sample code is as follows:

 var OJB = Jsb.reflection.callStaticMethod (ClassName, Methodnmae, arg1, arg2, ...);  

In the jsb.reflection.callStaticMethod method, we can directly call OC's static method by passing in the class name of OC, method name, parameter, and can get the return value of OC method.

Class
    • nativeocclass , as long as you introduce him to the project, then his class name is nativeocclass , you do not need to pass in its path.
    import <Foundation/Foundation.h>    @interface NativeOcClass : NSObject    +(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content;    @end
Method
    • js to OC Reflection only supports static methods of classes in OC.
    • callnativeuiwithtitle:andcontent: , do not omit the .
    • If it is a function without parameters, then he does not need to : , the following code, his method name is callnativewithreturnstring , because there is no parameter, he does not need to , in accordance with OC method.
    +(NSString *)callNativeWithReturnString;
Using the example
    • The following example code will be called above nativeocclass method, in the JS layer we only need to call this:
 var ret = Jsb.reflection.callStaticMethod (" Nativeocclass ",                                                "Callnativeuiwithtitle:andcontent:", "Cocos2d-js", "Yes! Native UI from Reflection ");  
    • Here is the implementation of this method in OC, which can be seen to pop up a native dialog box. and put title and content is set to the parameter you passed in and returns a Boolean return value.
 + (BOOL) Callnativeuiwithtitle: (NSString *) title andcontent: (NSString *) content{UIAlert View *alertview = [[Uialertview alloc] initwithtitle:title message:content delegate:self cancelbuttontitle:@ "Cancel"        otherbuttontitles:@ "OK", nil];        [Alertview show];    return true; }
    • At this point, you can ret accept the return value (TRUE) returned from OC in.
Attention

In the OC implementation, if the parameters of the method need to use float, int, bool, use the following types to convert:

    • float,int please use NSNumber type
    • < Span style= "" >bool please use bool type.
    • For example the following code, we pass in 2 floating-point numbers, and then calculate their merge return, we use nsnumber instead of int, float to go as the parameter type.
+(float) addTwoNumber:(NSNumber *)num1 and:(NSNumber *)num2{    float result = [num1 floatValue]+[num2 floatValue];    return result;}
    • The current parameters and return values support int, float, bool, string, and the remaining types are temporarily unsupported.

reprinted from:http://www.cocos2dx.net/post/254

"COCOS2D-JS Official Document" 23, how to use JS Direct call OC method on the iOS platform

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.