Vin*_*aka 4 c cocoa-touch function objective-c
我在viewController.m中使用了ac函数.
int abc(int a, char* b)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
我也有一个功能
-(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];
}
Run Code Online (Sandbox Code Playgroud)
现在我想callIncomingClass
从函数abc中调用.
你怎么建议我去做?
为什么我想从C函数调用Objective C方法,我不能创建一个按钮或在C函数中进行类似的处理.
以下代码是否有效:
int abc(int a, char* b)
{
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingClass];
}
Run Code Online (Sandbox Code Playgroud)
编辑:我正在做的事情的大图有一个ac库,即一个library.c和library.h文件.library.h文件有一个具有回调函数的结构.这些需要分配函数指针.所以我使用签名int abc(int,char*)的ac函数将被分配给struct中的回调函数.
此函数abc在ViewController.m中定义.理想情况下,我希望它在一个单独的文件中定义.但这也没关系.
所以现在,回调事件发生了,我想在View上创建一个带有一些动作的UIButton.由于我无法从ac函数创建UIButton,我正在调用ViewController类的目标C方法,它创建了UIButton.
希望能够清楚地了解我计划如何使用它.
由于其他人和我自己的说法,你的按钮没有显示:你需要现有的实例ViewController
.您正在创建一个全新的ViewController实例,它永远不会被带到屏幕上或推送等.
您可以使用指向现有实例的全局变量来完成您需要执行的操作.
这是你的.m应该是这样的:
#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;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2175 次 |
最近记录: |