Login Screen with Storyboarding possible?

南楼画角 提交于 2019-12-20 08:19:22

问题


I am playing around with the new iOS 5 features and trying to rewriting one of my apps as pure iOS 5 app using the new storyboarding feature.

To cut a long story short, I have a start screen where the app tries to connect to a server if the user saved some login data, if not, it should ask for them.

Here is how I would do it. I create a Viewcontroller which is doing the connection thing in the viewDidLoad method. If there is no login data or the login is not successful, I need a to do a manual segue to the login screen.

Now is this even possible, or do I need 2 story boards for that ?


回答1:


I have solved it by putting a login view without any segues (to or from it) like in the screenshot below:

Then, I used a custom class in the tab bar controller to show it whenever I need it.

In the tab bar controller class, I use 'viewDidLoad' to fire up the login view. To show the modal view, I do have a singleton thingy that stores some state, say BOOL isAuthenticated, where I do the magic:

- (void) performLoginIfRequired: (UIViewController *) source {

   if (!self.isAuthenticated) {

       NSLog(@"Is not authed");

       UIStoryboard *storyboard = [UIApplication sharedApplication].delegate.window.rootViewController.storyboard;

       UIViewController *loginController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];

       [source presentModalViewController:loginController animated:YES];

   } else {
       NSLog(@"Is authe");

   }
}

And, in my case, I wanted it to be shown when the app first starts, but also when it enters foreground again. So, I registered my tab bar controller with the notification center, so I get notified if the app is coming back:

-(void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(willEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

In the willEnterForeground method, I do:

-(void) willEnterForeground: (NSNotification *)notification {
    [[myStateThingy defaultState] performLoginIfRequired:self]; 
}



回答2:


It sounds like you need to use the performSegueWithIdentifier method. Make sure both views are in the same storyboard, link them together using a Push segue, and give that segue a name. Then, from your first view controller's code simply call the performSegueWithIdentifier to perform a manual segue.

Hope this helps!

See also: Conditionally following a segue

Cheers, Jesse L. Zamora




回答3:


I had this same issue, and I solved it simply by doing the following: Instead of trying to segue to a login screen(modally or push), I made the login screen my root view controller. In the login view controller's viewWillAppear method, I check if someone's logged in already. If so, I push my home screen:

// mutableFetchResults is an array with my persistent Credentials object
if ([mutableFetchResults count] > 0) { // Someone's already logged in
  [self performSegueWithIdentifier:@"Home" sender:self];
}

Also, in the Home screen view controller's viewWillAppear method, I hid the back button with this line, so the user can't go "back" to the login screen:

self.navigationItem.hidesBackButton = YES;

Finally, every page of my app has a "Sign Out" bar button on the top right. Signing out and putting the login screen up was as simple as this:

- (IBAction)signOutButtonPressed:(UIBarButtonItem *)sender {
  MyAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  [appDelegate signOutCurrentUser]; // this method in my app delegate deletes the current Credentials
  [self.navigationController popToRootViewControllerAnimated:YES];
}

Hope that was simple enough!




回答4:


After trying many different methods, I was able to solve this problem with this:

-(void)viewWillAppear:(BOOL)animated {

    // Check if user is already logged in
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"log"] intValue] == 1) {
        self.view.hidden = YES;
    }
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];

    // Check if user is already logged in
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    if ([[prefs objectForKey:@"log"] intValue] == 1) {
        [self performSegueWithIdentifier:@"homeSeg3" sender:self];
    }
}

-(void)viewDidUnload {
    self.view.hidden = NO;
}


来源:https://stackoverflow.com/questions/7913840/login-screen-with-storyboarding-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!