Dan Newns Dan Newns - 3 years ago 141
iOS Question

viewController appearing black when appearing

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.


Answer Source

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.


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download