1

I've a C file and a Objective C file.
Namely, C file being : CB.m and CB.h
CB.m contains a c function : callIncomings(). This function has to be a C function because, it is a callback function. (Its signature cannot be changed)

the Objective C files are, ViewController.h/.xib/.m and IncomingCall.h/.m/.xib.

Now, I've declared the following in ViewController.h

static ViewController* varViewController;

in viewDidLoad, I assign :

varViewController = self;

and I've the following code in CB.m

#include <stdio.h>
#import "ViewController.h"
#import "IncomingCall.h"

int callIncomings(int a, char* b)
{

    IncomingCall *obj = [[IncomingCall alloc]initWithNibName:@"IncomingCall" bundle:nil];
    //NSString *temp =
    //NSString* string =
    obj.tempAddress = [NSString stringWithFormat:@"%s" , b];

    [obj setModalTransitionStyle:UIModalTransitionStylePartialCurl];
    [varViewController presentModalViewController:obj animated:YES];

    [obj release];

    return 1;
}

This function is called in :

- (IBAction)DemoCall:(id)sender {
    callIncomings(1, "from C file");    
}

The project builds fine, but the view IncomingCall() is not getting displayed.

any ideas why??

EDIT : Also, I wanted to know, if I wanted to generalise the C function and load some different view say ABCD.view or IncomingCall view, how can I generalise this function.

EDIT 2 : This question can be a continuity from here : C function calling objective C functions

Some Clarifications : When I put the callIncoming function in the viewcontroller.m file, it worked fine. But now I want to place it separately, in another file and hence the CB.m file.
So that answers your doubt about varViewController. The DemoCall function is in the ViewController file, if you notice its a IBAction, so yes, the function IncomingCall is definitely getting called.

Inputs : Yes, like someone pointed out, its true, the varViewController seems to be NULL. Now what??!! Where am I going wrong?? .enter image description here

Community
  • 1
  • 1
anotherCoder
  • 712
  • 3
  • 11
  • 25
  • Both source files are Objective-C (`.m`) not C (`.c`). – trojanfoe Oct 17 '12 at 10:32
  • okie, so does changing the file extension resolve the problem?? i.e. changing Cb.m to CB.c??? – anotherCoder Oct 17 '12 at 10:35
  • 2
    Did you test your `varViewController` variable? Are you sure it has been initialized and not `nil`? You say it is called in `viewDidLoad` of `ViewController`, but has it been called? – Guillaume Oct 17 '12 at 10:36
  • 1
    No, it's correct. But you have a C function in an Objective-C source file. Have to run it in a debugger and checked if the callback is entered? – trojanfoe Oct 17 '12 at 10:37
  • Why `static varViewController` if you want it to be referenced externally? – trojanfoe Oct 17 '12 at 10:59
  • **static** so that its not changed during the course of the program, if once set. – anotherCoder Oct 17 '12 at 11:01
  • 1
    That sounds more like `const` than `static`. Are you sure that's what `static` means? – trojanfoe Oct 17 '12 at 11:04
  • Yes, thats correct. My usage is flawed. I've now made into a GLOBAL variable, which initialized in viewDidLoad of viewController. The function/callback is called at a button click event(for now, testing). The code works fine. Thank you @trojanfoe, for asking the right questions to resolve the errors step by step. – anotherCoder Oct 17 '12 at 11:17

2 Answers2

4

The issue is with the declaration of:

static ViewController* varViewController;

in ViewController.h.

The static keyword means that the variable will be created locally (it has internal linkage) in every source file that includes that header file. Therefore the varViewController that is used in CB.m is a different (uninitialized) pointer to the actual one in ViewController.m.

Correct this error with:

extern ViewController* varViewController;

(and make sure it's not defined as static within ViewController.m, else you'll get a link failure).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

Write following code...

#include <stdio.h>
#import "ViewController.h"
#import "IncomingCall.h"


static  int callIncomings(int a, char* b);
Prasad G
  • 6,702
  • 7
  • 42
  • 65