I’ve a c function in my viewController.m.
int abc(int a, char* b)
{
//do something
}
I also have a function
-(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];
}
Now I want to call callIncomingClass from within the function abc now.
How do you suggest I go about it??
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.
Should the following code work :
int abc(int a, char* b)
{
ViewController * tempObj = [[ViewController alloc] init];
[tempObj callIncomingClass];
}
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.
This function abc is defined in ViewController.m.
Ideally i wanted it to be defined in a separate file. but this is also okie.
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.
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.
Here’s what your .m should look like: