native.js在ios中怎么使用

2025-05-07 18:22:10
推荐回答(1个)
回答(1):

使用示例:
下面的示例代码将调用上面NativeOcClass的方法,在js层只需要这样调用:
var ret = jsb.reflection.callStaticMethod("NativeOcClass",
"callNativeUIWithTitle:andContent:",
"cocos2d-js",
"Yes! you call a Native UI from Reflection");

这里是这个方法在OC的实现,可以看到是弹出一个native的对话框。并把title和content设置成传入的参数,并返回一个boolean类型的返回值。
+(BOOL)callNativeUIWithTitle:(NSString *) title andContent:(NSString *)content{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:content delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alertView show];
return true;
}

此时,就可以在ret中接受到从OC传回的返回值(true)了。
注意:
在OC的实现中,如果方法的参数需要使用float、int、bool的,请使用如下类型进行转换:
float,int 请使用NSNumber类型
bool请使用BOOL类型。
例如下面代码,传入2个浮点数,然后计算他们的合并返回,使用NSNumber而不是int、float去作为参数类型。
+(float) addTwoNumber:(NSNumber *)num1 and:(NSNumber *)num2{
float result = [num1 floatValue]+[num2 floatValue];
return result;
}