C function calling objective C functions

后端 未结 3 1535
醉酒成梦
醉酒成梦 2020-12-09 23:19

I\'ve a c function in my viewController.m.

int abc(int a, char* b)
{
//do something
}

I also have a function

-(void) call         


        
3条回答
  •  醉酒成梦
    2020-12-10 00:05

    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:

    #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;
    }
    

提交回复
热议问题