ユーザーがサーバーにログインしているかどうかに基づいて、異なるテンプレートが表示されます。ユーザーがアプリケーションサーバーにログインしていない場合はUIViewController
、LoginViewController [ UITabBarViewController
]を表示する必要があります。それ以外の場合は、TabBarViewcontroller [ ]を表示します。
初めてストーリーボードを使っているので。ストーリーボード内でこのタイプの状態を処理するようにアドバイスできる人はいますか?
私は次のようにそれをしました。
まず、LoginViewControllerと呼ばれるものを作成します。ログインロジックを実装します。ルートコントローラーはUITabBarControllerです。
現在AppDelegateにあります:
- (void) doLogin:(NSDictionary *)dict
{
TransactionsViewController *transactions;
BalancesViewController *balances;
ProfileViewController *profile;
UINavigationController *navi;
UITabBarController *root;
root = (UITabBarController *)self.window.rootViewController;
navi = [root.viewControllers objectAtIndex:0];
transactions = [navi.viewControllers objectAtIndex:0];
transactions.delegate = self;
navi = [root.viewControllers objectAtIndex:1];
balances = [navi.viewControllers objectAtIndex:0];
balances.delegate = self;
navi = [root.viewControllers objectAtIndex:2];
profile = [navi.viewControllers objectAtIndex:0];
profile.delegate = self;
[transactions loadDataFromLogin:dict];
[balances loadDataFromLogin:dict];
[profile loadDataFromLogin:dict];
}
- (void) showLoginView
{
assert(loginController == nil);
assert(activityView == nil);
UITabBarController *tabbar = (UITabBarController *)self.window.rootViewController;
loginController = [tabbar.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
loginController.delegate = self;
[tabbar presentModalViewController:loginController animated:YES];
}
- (void) loginWithLogin:(NSDictionary *)dict relogin:(BOOL)relogin
{
NSString *_login = [dict valueForKey:@"email"];
NSString *_pass = [dict valueForKey:@"pass"];
NetworkOperation *op = [NetworkOperation operationLogin:_login pass:_pass];
[NetworkOperation enqueueOperation:op observer:self];
if (!relogin && !loginController)
[self doLogin:dict];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
session = nil;
loginController = nil;
return YES;
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
NSString *login = [[NSUserDefaults standardUserDefaults] stringForKey:@"email"];
NSString *pass = [[NSUserDefaults standardUserDefaults] stringForKey:@"pass"];
NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
if ((login == nil || login.length == 0) ||
(pass == nil || pass.length == 0) ||
(name == nil || name.length == 0)) {
if (!loginController) {
[self.window makeKeyAndVisible];
[self performSelector:@selector(showLoginView) withObject:nil afterDelay:0.1];
}
} else {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:login, @"email",
pass, @"pass", name, @"name", nil];
[self loginWithLogin:dict relogin:(session != nil)];
}
}
そのため、アプリデリゲートは、パスとログインが保存されているかどうかを確認します。はいの場合、それらを使用してログインし、ルートビューコントローラーに切り替えます。私の場合、残高や取引などのタブをいくつか表示するためにタブが付けられています。
ログインとパスがない場合は、ルートビューコントローラーの上にログインビューコントローラーを表示します。
ここで@sessionは現在のセッションです。コードに示されているように、ログインはキューに入れられた操作を使用しています。
詳細が必要な場合は、pingを送信してください。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加