C函数调用的目标C函数 [英] C function calling objective C functions
问题描述
我在我的viewController.m C函数。
I've a c function in my viewController.m.
int abc(int a, char* b)
{
//do something
}
我也有一个函数
-(void) callIncomingClass
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
//set the button's title
[button setTitle:@"Click Me!" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:button];
}
现在我想从现在的功能ABC内调用 callIncomingClass
。
Now I want to call callIncomingClass
from within the function abc now.
你怎么建议我去了解它?
How do you suggest I go about it??
为什么我要打电话从C功能的目标C的方法是,我已经不能创建一个按钮或做一些处理一样,在C函数。
Why I want to call an Objective C method from the C function is, I've cannot create a button or do some processing like that in the C function.
应在下述code的工作:
Should the following code work :
int abc(int a, char* b)
{
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingClass];
}
编辑:我在做什么大图片
有一个C库,即一个LIBRARY.C和library.h文件。该library.h文件中有一个具有回调函数的结构。这些都需要与函数指针分配。所以,我的签名INT ABC(INT,CHAR *),它是要分配给在结构中的回调函数的C函数。
edit : the big picture of what I am doing There is a c library, i.e. a library.c and library.h file. The library.h file has a struct that has callback functions. These need to be assigned with function pointers. so I've a c function with the signature int abc(int,char*) that is to be assigned to the callback function in the struct.
此功能ABC在ViewController.m定义。
理想的情况是我希望它在一个单独的文件中定义。不过这也是奥基。
This function abc is defined in ViewController.m. Ideally i wanted it to be defined in a separate file. but this is also okie.
所以,现在,回调事件发生,我想创建一个UIButton上查看一些动作。由于我无法从C函数创建一个UIButton,我调用的ViewController类的客观C法,即创建的UIButton。
So now, the callback event happens, I want to create a UIButton with some action on the View. As I can't create a UIButton from a c function, i am calling a objective C method of the ViewController class, that creates the UIButton.
。希望清除图片为我计划如何使用这一点。
Hope that clears the picture as to how I am planning to use this.
推荐答案
您按钮不会显示,因为别人和自己都在说:你需要的的ViewController $ C的现有实例$ C>。您正在创建的ViewController,这是从来没有带来在屏幕上或推一个全新的实例等。
Your button doesn't show because of what others and myself were saying: you need the existing instance of the ViewController
. You are creating an entirely new instance of the ViewController, which is never brought on screen or pushed, etc.
您可以完成你需要使用一个指向现有实例一个全局变量做什么。
You can accomplish what you need to do by using a global variable that points to your existing instance.
下面是您的m应该是什么样子:
Here's what your .m should look like:
#import "ViewController.h"
static ViewController *viewController = nil;
@implementation ViewController
- (id)init {
if ((self = [super init])) {
viewController = self;
}
return self;
}
- (void)dealloc {
viewController = nil;
}
-(void) callIncomingCreateButton {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//set the position of the button
button.frame = CGRectMake(100, 170, 100, 30);
//set the button's title
[button setTitle:@"Click Me!" forState:UIControlStateNormal];
//add the button to the view
[self.view addSubview:button];
}
- (IBAction)DemoCall:(id)sender {
callIncoming(1, "a");
}
@end
int callIncoming(int a, char* b) {
[viewController callIncomingCreateButton];
return a;
}
这篇关于C函数调用的目标C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!