viewController appearing black when appearing
Asked Answered
M

3

8

Hi All I'm new to iOS development and am currently having an issue with something, I'm trying to make a login form appear modally if a user is not logged in.

I'm using NSUserDefaults to store an id which I'm simply checking if it exists. If it doesnt then I would want the UIViewController to appear.

So far I have the following code in my UINavigationController which is within a UITabViewController , I'm trying to get the UIViewController to appear above the First UINavigationController (the one that is selected), at the moment the animation is happening but there is only a black screen even though the Login screen has all the relevant text boxes etc already created. If I set this Login screen as the initial view to load, it loads up fine.

This is the code that appears in the First viewcontroller of the UINavigationController.

 NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (![prefs stringForKey:@"id"]){
    LoginViewController *loginController = [[LoginViewController alloc] init];
    [self presentModalViewController:loginController animated:YES];
} else {
    [self loadCoffeeUserOrders:[prefs stringForKey:@"id"]];  
}

the modal view is loading up but at the moment it just appears black, I've tried creating a new Login Screen and the same thing happens: nothing but a black screen.

hope there is enough information there for someone to understand what might be going on, I am very new to iOS development so if I've made some mistakes it would be nice to know where I'm going wrong.

thanks

Morbilli answered 4/11, 2011 at 19:0 Comment(3)
Make sure that your LoginViewController's view is connected in the XIB file, set a break point in LoginViewController's viewDidLoad and make sure it is doing what you expect.Virus
Is your login view implemented in loadView or have you set up for a nib for this?Vo
i have just added a NSLog in my viewDidLoad and it is logging it out as expected. @FirozeLafeer im using storyboard mode in Xcode so i have the view already in the storyboard.Morbilli
V
7

So, the LoginViewController you are creating here is a different instance from the one that you set up in your storyboard. I think what you want to cause the one in the storyboard to be loaded and presented.

The easiest way is to create a segue in the storyboard from your first view controller to the login view controller that you created there. You can ctrl-drag from one view controller to the other and select "Modal" as the type of segue. Then go to the inspector for that segue and give it an identifier. Let's say you call it "segueToLogin".

Then to perform that segue from your code just do something like this:

  if (![prefs stringForKey:@"id"]){

    [self performSegueWithIdentifier:@"segueToLogin" sender:self];

  }

Hope that helps.

EDIT

So just to answer your question, imagine this:

You have this great idea for an app, so you take a sheet of paper off your pad and you sketch out this great design. Then your colleagues come by so you rip off a new blank sheet of paper and show them that instead. They won't be impressed.

Same thing happened here. You set up a view controller in your storyboard with all your views. But then when it came time to show it, you pulled out a new totally blank view controller and showed that to the user instead. By pulling and actuating the segue instead, you end up loading the actual view controller instance you wanted.

Now on segues, you create those in the storyboard to allow the user to navigate through the scenes. Sometimes those segues are directly attached to a button or something. But in this case you don't want to ask the user to press a button or something to present the login vc, so you are performing the segue yourself.

Hope that makes sense.

Vo answered 4/11, 2011 at 20:44 Comment(2)
Hi @Firoze Lafeer thank you very much for that it worked a treat, ive ticked your answer as correct (i hope thats what i need to do, its my first post on there) so should i be doing this performSegueWithIdentifier action everytime want to make a push, modal when i create a similar controller that is a different instance to what is on the storyboard? Im still trying to get my head around it all so thanks for that answer really helped.Morbilli
Not sure we're saying the same thing. I added a bit to my answer, hope that makes sense.Vo
T
3

I had a similar issue but the resolution was not the one listed above. The problem resided in the actual new view controller ".m" file (the one I'm trying to push/modal on). There is a method called loadView inside and if you leave it there XCode will ignore the xib you have in the storyboard and expect you to lay out the view. Remove that function and it will look at the xib instead.

Spent too long on this stupid bug, hope this can help save someone else's sanity.

Trude answered 6/4, 2012 at 4:42 Comment(0)
M
0
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
LoginViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
Mistook answered 28/2, 2015 at 6:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.