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

8
  • Both source files are Objective-C (.m) not C (.c). Commented Oct 17, 2012 at 10:32
  • okie, so does changing the file extension resolve the problem?? i.e. changing Cb.m to CB.c??? Commented Oct 17, 2012 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? Commented Oct 17, 2012 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? Commented Oct 17, 2012 at 10:37
  • 1
    That sounds more like const than static. Are you sure that's what static means? Commented Oct 17, 2012 at 11:04

2 Answers 2

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).

Sign up to request clarification or add additional context in comments.

Comments

0

Write following code...

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


static  int callIncomings(int a, char* b);

1 Comment

A little more explanations Prasad, would help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.