IOS登录注册回家的最佳做法

时间:2016-11-04 08:45:44

标签: ios iphone login uiviewcontroller

我正在构建一个iphone应用程序,在我的AppDelegate中,我使用此代码加载我的HomeView(如果用户已登录):

if(isLoggedIn==YES)
{
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    [self.window setRootViewController:(HomeViewController *)[sb instantiateViewControllerWithIdentifier:@"homeScreen"]];

}

在HomeViewController中,当我的用户注销时,我使用此代码切换到LoginViewController:

UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.view.window.rootViewController = (LoginViewController *)[sb instantiateViewControllerWithIdentifier:@"loginScreen"];

你能告诉我最佳做法吗?

由于

1 个答案:

答案 0 :(得分:2)

///Appdelegate.h

@property (strong, nonatomic) UINavigationController *navigationController;

/////Appdelegate.m

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

self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
 if(!isLoggedIn)
 {
 UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
        _navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];

   }

else
{
  UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"homeScreen"];
        _navigationController=[[UINavigationController alloc]initWithRootViewController:viewController];

}
 self.window.rootViewController = self.navigationController;
        [self.window makeKeyAndVisible];    
}
////logout_action

-(IBAction)myprofile:(id)sender
{
islogged=NO;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
   MyprofileViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"loginScreen"];
    [self.navigationController pushViewController:viewController animated:YES];
}