• 【cocos2d-js官方文档】二十三、如何在IOS平台上使用js直接调用OC方法


    在Cocos2d-JS v3.0 RC2中,与Android上js调用Java一样,Cocos2d-JS也提供了在iOS和Mac上js直接调用Objective-C的方法,示例代码如下:

        var ojb = jsb.reflection.callStaticMethod(className, methodNmae, arg1, arg2, .....);
    

    jsb.reflection.callStaticMethod方法中,我们通过传入OC的类名,方法名,参数就可以直接调用OC的静态方法,并且可以获得OC方法的返回值。

    • 参数中的类名,只需要传入OC中的类名即可,与Java不同,类名并不需要路径。比如你在工程底下新建一个类NativeOcClass,只要你将他引入工程,那么他的类名就是NativeOcClass,你并不需要传入它的路径。
    1.  
      import <Foundation/Foundation.h>
    2.  
      @interface NativeOcClass : NSObject
    3.  
      +(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content;
    4.  
      @end

    方法

    • js到OC的反射仅支持OC中类的静态方法。
    • 方法名比较要需要注意,我们需要传入完整的方法名,特别是当某个方法带有参数的时候,你需要将他的:也带上。根据上面的例子。此时的方法名字是callNativeUIWithTitle:andContent:,不要漏掉了他们之间的:。
    • 如果是没有参数的函数,那么他就不需要:,如下代码,他的方法名是callNativeWithReturnString,由于没有参数,他不需要:,跟OC的method写法一致。
        +(NSString *)callNativeWithReturnString;
    

    使用示例

    • 下面的示例代码将调用上面NativeOcClass的方法,在js层我们只需要这样调用:
    1.  
      var ret = jsb.reflection.callStaticMethod("NativeOcClass",
    2.  
      "callNativeUIWithTitle:andContent:",
    3.  
      "cocos2d-js",
    4.  
      "Yes! you call a Native UI from Reflection");
    • 这里是这个方法在OC的实现,可以看到是弹出一个native的对话框。并把titlecontent设置成你传入的参数,并返回一个boolean类型的返回值。
    1.  
      +(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{
    2.  
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    3.  
      [alertView show];
    4.  
      return true;
    5.  
      }
    • 此时,你就可以在ret中接受到从OC传回的返回值(true)了。

    注意

    在OC的实现中,如果方法的参数需要使用float、int、bool的,请使用如下类型进行转换:

    • float,int 请使用NSNumber类型
    • bool请使用BOOL类型。
    • 例如下面代码,我们传入2个浮点数,然后计算他们的合并返回,我们使用NSNumber而不是int、float去作为参数类型。
    1.  
      +(float) addTwoNumber:(NSNumber *)num1 and:(NSNumber *)num2{
    2.  
      float result = [num1 floatValue]+[num2 floatValue];
    3.  
      return result;
    4.  
      }
    • 目前参数和返回值支持 int, float, bool, string,其余的类型暂时不支持。

    转载自:https://blog.csdn.net/qinning199/article/details/42045765

  • 相关阅读:
    Ubuntu Git GUI工具GitKraken安装
    轻松理解String.intern()
    Ubuntu MySQL安装
    稳定与不稳定的人生(转自知乎)
    【推荐】我们这一代人的困惑
    没关系,因为你是好人呀
    论文--Topic-Sensitive PageRank
    论文笔记-Mining latent relations in peer-production environments
    Open Source Book For ML
    LeetCode-Populating Next Right Pointers in Each Node
  • 原文地址:https://www.cnblogs.com/wodehao0808/p/11929646.html
Copyright © 2020-2023  润新知