最近开发项目的时候,遇到C函数需要调用objective-C函数,而C函数中不能够直接调用,不支持直接[self 函数]。
可以有以下两种方法来调用:
1、利用单例模式,把要调用的objective-c函数写在一个单例模式当中,然后在C函数中调用,如下:
void test1 (int32 x) {
[MyController instances].myObjectCFun setIntX:x];
}
2、利用通知NSNotification,在C函数中用通知来调用objective-c函数,如下:
void test2 (int32 x) {
NSNumber *n = [NSNumber numberWithInt:x];
NSNotification *notification = [NSNotication notificationWithName:@"mynotificaion" object:n];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}