C function calling C objective functions

I have 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 title [button setTitle:@"Click Me!" forState:UIControlStateNormal]; //add the button to the view [self.view addSubview:button]; } 

Now I want to call callIncomingClass from the abc function now.

How do you suggest me to do this?

Why I want to call the Objective-C method from a C function, I cannot create a button or do some processing similar to this C function.

If the following code works:

 int abc(int a, char* b) { ViewController * tempObj = [[ViewController alloc] init]; [tempObj callIncomingClass]; } 

edit: big picture of what I am doing. There is a c-library, i.e. library.c and library.h. The library.h file has a structure that has callback functions. They must be assigned function pointers. so I have a c-function with the signature int abc (int, char *) that needs to be assigned to the callback function in the structure.

This abc function is defined in ViewController.m. Ideally, I wanted it to be defined in a separate file. but that too is normal.

So, now the callback event occurs, I want to create a UIButton with some actions in the view. Since I cannot create a UIButton from a c function, I call the C object method of the ViewController class, which creates the UIButton.

Hope that clears the image regarding how I plan to use this.

+4
source share
3 answers

Your button does not appear due to what others and I have said: you need an existing instance of ViewController . You create a completely new instance of ViewController, which is never displayed on the screen or pressed, etc.

You can accomplish what you need to do using a global variable that points to your existing instance.

Here is 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 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; } 
+5
source

You need to access the correct ViewController* / instance, not the old one. If your C-signature of the function does not allow passing some arbitrary data using void* or similar, then you need to use the class method and static variable to hold the temporary pointer as follows.

In the Objective-C file:

 static ViewController* cLibReceiver = NULL; +(void) callIncomingClassOuter { [cLibReceiver callIncomingClass]; } -(void) beforeTriggerCLibrary { cLibReceiver = self; } 

and in your (other) C object file with abc() in:

 int abc(int a, char* b) { [ViewController callIncomingClassOuter]; } 

If you are running the C library, you need to do this:

 [theViewController beforeTriggerCLibrary]; // could be self, depending on context c_library_call(); // optionally set cLibReciever back to NULL. 

note: Perhaps I have some syntax errors in the method headers, etc., I am not completely familiar with the objective conventions for calling C. class methods are definitely + , though.

Note 2: You are not allowed to distribute the system class as follows: you may need to define your own subclass. Again, not familiar with Objective C.

+1
source

Assuming you are in the source Objective-C file, calling an Objective-C function from a C function works just like calling an Objective-C function from any other Objective-C function. In any case, if you have a ptr for the object you want to include the function on, you write

 [ptr callIncomingClass]; 

therefore, of course, in any case, you need to somehow point to a pointer to the object you want to call the function on. If you use the Objective-C function (ia instance method), the usual "source" for such pointers is: (a) an implicit "I" pointer, when you call the method on the same object as the currently running metid, (b) what- then the instance variable of the object on which the current method was called, (c) the argument of the current function or method, (d) the global variable or the result of some other function call. In plain C, you can use (c) and (d), but not (a) and (b), because you don't have self .

0
source

Source: https://habr.com/ru/post/1439315/


All Articles