可能的重复项:
调用目标 C 函数的 C 函数
我已经在ViewController.m中完成了以下代码
-(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");
}
int callIncoming(int a, char* b)
{
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingCreateButton];
return a;
}
但是UIButton仍然没有显示,我在这里错过了什么。
编辑 :这适用于 iOS是的,该函数确实被调用了。我放了一个断点并逐步通过它。
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingClass];
从您的示例中很难看出,但是假设-callIncomingClass
实际上是ViewController
定义的一部分,并且callIncoming()
是从其他地方调用的,则可能存在的问题是tempObj
不是当前的视图控制器。您需要将tempObj
推送到导航堆栈上,以模式方式呈现它,或者使其成为活动视图控制器,以便加载其视图。如果未加载其视图,则没有要添加按钮的视图,即使它具有视图,该视图也不会显示在窗口中。