亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

有條件地從AppDelegate的情節提要中的不同位置開始

有條件地從AppDelegate的情節提要中的不同位置開始

明月笑刀無情 2019-12-09 14:11:19
我有一個故事板設置了正常的登錄名和主視圖控制器,主視圖控制器是登錄成功后用戶導航到的視圖控制器。我的目標是如果身份驗證(存儲在鑰匙串中)成功,則立即顯示主視圖控制器,如果身份驗證失敗,則顯示登錄視圖控制器?;旧?,我想在AppDelegate中執行以下操作:// url request & response work fine, assume success is a BOOL here// that indicates whether login was successful or notif (success) {          // 'push' main view controller} else {          // 'push' login view controller}我知道方法performSegueWithIdentifier:但是該方法是UIViewController的實例方法,因此無法從AppDelegate中調用。如何使用現有的情節提要板來完成此任務?編輯:情節提要的初始視圖控制器現在是導航控制器,沒有連接任何東西。我使用了setRootViewController:區別,因為MainIdentifier是UITabBarController。這就是我的代碼:- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{            BOOL isLoggedIn = ...;    // got from server response    NSString *segueId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];    UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:segueId];    if (isLoggedIn) {        [self.window setRootViewController:initViewController];    } else {        [(UINavigationController *)self.window.rootViewController pushViewController:initViewController animated:NO];    }    return YES;}歡迎提出建議/改進!
查看完整描述

3 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

我假設您的情節提要板被設置為“主情節提要板”(UIMainStoryboardFileInfo.plist中的鍵)。在這種情況下,UIKit將加載情節提要,并將其初始視圖控制器設置為窗口的根視圖控制器,然后再發送application:didFinishLaunchingWithOptions:到AppDelegate。


我還假設情節提要中的初始視圖控制器是導航控制器,您要將主視圖或登錄視圖控制器推入該導航控制器。


您可以向窗口詢問其根視圖控制器,并將performSegueWithIdentifier:sender:消息發送給它:


NSString *segueId = success ? @"pushMain" : @"pushLogin";

[self.window.rootViewController performSegueWithIdentifier:segueId sender:self];


查看完整回答
反對 回復 2019-12-09
?
絕地無雙

TA貢獻1946條經驗 獲得超4個贊

我對這里提出的一些解決方案感到驚訝。


故事板中實際上不需要虛擬的導航控制器,無需在viewDidAppear:或任何其他黑客上隱藏視圖并觸發segues。


如果沒有在plist文件中配置情節提要,則必須自己創建窗口和根視圖控制器:


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

{        

    BOOL isLoggedIn = ...;    // from your server response


    NSString *storyboardId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];

    UIViewController *initViewController = [storyboard instantiateViewControllerWithIdentifier:storyboardId];


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

    self.window.rootViewController = initViewController;

    [self.window makeKeyAndVisible];


    return YES;

}

如果在應用程序的plist中配置了故事板,則在調用application:didFinishLaunching:時將已經設置了窗口和根視圖控制器,并且將在窗口上為您調用makeKeyAndVisible。


在這種情況下,它甚至更簡單:


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

{        

    BOOL isLoggedIn = ...;    // from your server response


    NSString *storyboardId = isLoggedIn ? @"MainIdentifier" : @"LoginIdentifier";

    self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:storyboardId];


    return YES;

}


查看完整回答
反對 回復 2019-12-09
?
慕虎7371278

TA貢獻1802條經驗 獲得超4個贊

如果您的情節提要的入口點不是UINavigationController:


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



    //Your View Controller Identifiers defined in Interface Builder

    NSString *firstViewControllerIdentifier  = @"LoginViewController";

    NSString *secondViewControllerIdentifier = @"MainMenuViewController";


    //check if the key exists and its value

    BOOL appHasLaunchedOnce = [[NSUserDefaults standardUserDefaults] boolForKey:@"appHasLaunchedOnce"];


    //if the key doesn't exist or its value is NO

    if (!appHasLaunchedOnce) {

        //set its value to YES

        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"appHasLaunchedOnce"];

        [[NSUserDefaults standardUserDefaults] synchronize];

    }


    //check which view controller identifier should be used

    NSString *viewControllerIdentifier = appHasLaunchedOnce ? secondViewControllerIdentifier : firstViewControllerIdentifier;


    //IF THE STORYBOARD EXISTS IN YOUR INFO.PLIST FILE AND YOU USE A SINGLE STORYBOARD

    UIStoryboard *storyboard = self.window.rootViewController.storyboard;


    //IF THE STORYBOARD DOESN'T EXIST IN YOUR INFO.PLIST FILE OR IF YOU USE MULTIPLE STORYBOARDS

    //UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YOUR_STORYBOARD_FILE_NAME" bundle:nil];


    //instantiate the view controller

    UIViewController *presentedViewController = [storyboard instantiateViewControllerWithIdentifier:viewControllerIdentifier];


    //IF YOU DON'T USE A NAVIGATION CONTROLLER:

    [self.window setRootViewController:presentedViewController];


    return YES;

}

如果您的情節提要的入口點是UINavigationController替換項:


//IF YOU DON'T USE A NAVIGATION CONTROLLER:

[self.window setRootViewController:presentedViewController];

與:


//IF YOU USE A NAVIGATION CONTROLLER AS THE ENTRY POINT IN YOUR STORYBOARD:

UINavigationController *navController = (UINavigationController *)self.window.rootViewController;

[navController pushViewController:presentedViewController animated:NO];


查看完整回答
反對 回復 2019-12-09
  • 3 回答
  • 0 關注
  • 625 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號