johnyu
johnyu

Reputation: 2151

presentViewController makes a black screen

I was searching quite long for solutions of my problem, but most of them were useless in my case, since almost all of them were about mistakes done in IB, which I am not using in this project. To the point:

I have an XCode project generated with Unity and I wanted to add another view controller with a UIWebView in it on top of the UnityViewController. Since Unity generates project that doesn't use UINavigationController, I couldn't use pushViewController and decided to use presentViewController:... instead. Problem is, when I try to do it, I only see a blackscreen. If I call dismissViewController I see UnityViewController again, so that works fine. Problem is, it's like the presentViewController doesn't add my vc's view. I used breakpoints to see if viewDidLoad is being called and it isn't. Same goes for -(void)loadView and -(UIView *)view. Here's the code that I use to add my vc:

WebViewController *webViewController = [[WebViewController alloc] init];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:webViewController animated:YES];

I checked if webViewController is nil and it isn't. That rootViewController is the UnityViewController, I am 100% sure of it.

This is the Unity-generated code that creates the view hierarchy:

_mainDisplay = [[DisplayManager Instance] mainDisplay];
[_mainDisplay->window makeKeyAndVisible];

static bool _ClassInited = false;
if(!_ClassInited)
{
    AddOrientationSupportDefaultImpl([UnityViewController class]);
    _ClassInited = true;
}

_viewController = [[UnityViewController alloc] init];
_viewController.wantsFullScreenLayout = TRUE;
_viewController.view = _mainDisplay->view;

and later in another method:

[_mainDisplay->window addSubview: _mainDisplay->view];
_mainDisplay->window.rootViewController = _viewController;
[_mainDisplay->window bringSubviewToFront: _mainDisplay->view];

Could the black screen be caused by the lack of UINavigationController?

This is probably irrelevant to my problem, but I also have a menu that I need to be visible all the time in my app and I made it a subview of keyWindow.

P.S. I also tried presenting a UINavigation controller with the webViewController set as rootvc, but all I got was a a black screen with a navigation bar.

P.S.S. I use Vuforia in my Unity project and therefore I can't run it in simulator.

Edit:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];

    UIImageView *icon = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Icon.png"]];
    icon.frame = CGRectMake(50, 50, 200, 200);
    [self.view addSubview:icon];
}

Breakpoint is on the line where I change backgroundColor. It isn't called.

Edit2: With breakpoints I found out that no methods of my WebViewController subclass are called, not even init. It's like xcode is ignoring existence of that class. I only edited viewDidLoad method, al the rest is as in the default UIViewController subclass template. No changes to WebViewController.h either.

Edit3: Fixed it now. Changing WebViewController class name to a different one helped. The only logical explanation I can think of is that WebViewController was already implemented before in one of the source files, but I didn't do it myself. Could be in one of the libraries maybe (?). Anyway, thanks for help, I'm accepting GuillaumeA's answer since thanks to his suggestions I found out that it isn't actually a problem of the view.

Upvotes: 2

Views: 9202

Answers (2)

Guillaume Algis
Guillaume Algis

Reputation: 11016

It's hard to know where your problem comes from using the info you provided.

It could just be a normal behavior. When creating a new instance of UIViewController (I assume your WebViewController is a subclass), it create a blank root UIView for it.

By 'blank' I mean that the view have no subview, but also no background color (nil). This makes the view appear black, and - for some reason - prevent the view from animating when displayed.

To test if my assumption is correct, try to change the background color of the WebViewController view :

WebViewController *webViewController = [[WebViewController alloc] init];
webViewController.view.backgroundColor = [UIColor redColor];
[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentModalViewController:webViewController animated:YES];

Note: presentModalViewController:animated: is now deprecated in iOS 6.0, so you might want to replace it with presentViewController:animated:completion::

[[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:webViewController animated:YES completion:nil];

Upvotes: 8

Lithu T.V
Lithu T.V

Reputation: 20021

Try to initialize like this

WebViewController *webViewController=[[WebViewController alloc]initWithNibName:@"WebViewController" bundle:[NSBundle mainBundle]];

Note : assumed your nib file is having name :WebViewController.xib

Upvotes: 0

Related Questions