User7723337
User7723337

Reputation: 12018

Display new view controller from a view controller

I am writing one iPhone application which is integrated with CPP code, I am using C interface file as common interface in-between Objective-C and CPP code.

I am trying to display a new view from my current view by use of following code:

viewTowController *screen = [[viewTwoController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:screen animated:YES];

I have used this code previously also but it was on button click event and it worked perfectly fine, but i need to display some views based on events generated from CPP code (on callback functions which are getting invoked from CPP).

When i call this code from a CPP call back function like below:

-(void) displayView
{
    viewTwoController *screen = [[viewTwoController alloc] initWithNibName:nil bundle:nil];
    [self presentModalViewController:screen animated:YES];
}

//-- C Interface Function Implementation --//
void onDisplayView(void *self)
{
    [(id) self displayView]; 
}

I am unable to see the view added to the screen and i am get bunch of below errors in the console logs window.

2012-12-18 18:03:02.128 p2pwebrtc[5637:5637] *** _NSAutoreleaseNoPool(): Object 0x4f04520 of class UIView autoreleased with no pool in place - just leaking
Stack: (0x305a2e6f 0x30504682 0x309778ac 0x7db6 0x7d28 0x33bb 0x6fb0 0x1af865 0x1afb4a 0x1afc5e 0x62a1 0x373e 0x4672 0x39937f 0x3ca1e4 0x3ca265 0x3cc6 0x926d8155 0x926d8012)

AM I doing anything wrong in this one, or there is other way of doing it?

UPDATE **

As suggested in the answers I need to execute this code in UI Thread and for that i need to do below code :

dispatch_async(dispatch_get_main_queue(), ^{
    [(id) self displayView];
});

When i call this code in Objective-C function OR CPP function i get ERROR 'dispatch_get_main_queue' was not declared in this scope.

Also i am using OSX version 10.5.8 and Xcode version 3.1.3

Upvotes: 0

Views: 122

Answers (1)

Art Gillespie
Art Gillespie

Reputation: 8757

Ensure you're calling into UIKit on the main thread, like so:

void onDisplayView(void *self) {
    dispatch_async(dispatch_get_main_queue(), ^{
        [(id) self displayView];
    });
}

If you're targeting an older version of iOS (pre-GCD) you could do this instead

// assuming `self` inherits from NSObject, of course
void onDisplayView(void *self) {
    [self performSelectorOnMainThread:@selector(displayView) withObject:nil waitUntilDone:NO];
}

Upvotes: 1

Related Questions