Easy way to override which screen shows up first i

2019-08-30 10:52发布

Right now I'm trying storyboards out, and I have my UITableViewController as my rootViewController. Now on some instances if my user is not logged in, I want another UIViewController to appear first. I understand I can perform a segue to it, but from my understanding the TableView will still try to get loaded, which is not what I want unless they supply info on this UIViewController that I'm trying to get to appear first (if say a key doesn't exist in NSUserDefaults) for example.

So my question is, is there an easy solution I can maybe add to my appDelegate to "override" the rootViewController from storyboard, or appear before it, then have a button simply on it to dismiss back to that rootViewController in storyboard?

Thanks!

3条回答
趁早两清
2楼-- · 2019-08-30 11:18

Really similar to this question: UIStoryboard load from app delegate

You should set the Storyboard ID in the Identity section of the View Controller in Interface Builder. Then you can get that screen via

UIViewController *viewControllerToShow = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];

You will end up with something like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

   UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];

       UIViewController *vc = nil;
       if (someKindOfCheck) {
           vc = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
       }
       else {
           vc =[storyboard instantiateInitialViewController];
       }

       // Set root view controller and make windows visible
       self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
       self.window.rootViewController = vc;
       [self.window makeKeyAndVisible];

   return YES;
}
查看更多
成全新的幸福
3楼-- · 2019-08-30 11:20

An easy way would be to set your table sections to 0 if the user isn't logged in, for example;

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger numberOfSections = 0;
if (userLoggedIn) 
{
  numberOfSections = 1;
}   

return numberOfSections;
}
查看更多
够拽才男人
4楼-- · 2019-08-30 11:26

You could:

  • Use a different initial view controller (you can specify this in the storyboard). In that view controller, check if the user is logged in. If they are, just transition directly to the table view controller via a manually triggered segue. If not, display the login screen.

  • Subclass UITableViewController. Check for the login in viewDidLoad. If not, present a modal login view controller.

You probably don't want to do it in the app delegate since that would require you to manually load the storyboard, which means unnecessary code.

查看更多
登录 后发表回答