2016-11-04 41 views
0

我建立一個iPhone應用程序,在我的AppDelegate我使用此代碼加載我HomeView(如果用戶已登錄):的登錄註冊家裏IOS最佳實踐

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"]; 

您能否告訴我最佳實踐?

感謝

+0

設置homeview控制器RootView和現在的登錄屏幕,如果用戶沒有登錄,註銷按鈕點擊後再次出現一個LoginView爲模態的視圖 – Vinodh

+0

您NSUserDefaults的應該爲這個 –

回答

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]; 
}