簡體   English   中英

在當前視圖控制器出現之前到達另一個視圖控制器

[英]Got to another view controller before the current view controller appears

我試圖在當前視圖控制器出現之前轉到新的視圖控制器,但似乎無法得到它。 基本上我的應用程序,在啟動頁面上啟動並檢查是否有已保存的NSUserDefaults,如果是,則打開主視圖控制器。 但是,我不希望用戶在看到主視圖之前看到該視圖,因為它們已經是用戶,但它不是用戶友好的。 我希望當前用戶直接查看啟動圖像,然后查看主視圖。 但是,我無法真正做到這一點。 這就是我所做的,它會產生不良影響。

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];

if(user.length>0){
    NSLog(@"Going straight to main %s", "Yes");
    [self performSegueWithIdentifier:@"startMain" sender:self];

  }
}

我怎么解決這個問題?

為了實現你想要的,即根本看不到第一個viewController,你必須在轉換中刪除動畫。 沒有動畫,用戶將看到第二個ViewController是第一個呈現的。

您可以通過多種方式實現這一目標。 其中一個(假設它是模態),將在故事板中創建一個新的segue,具有不同的標識符,沒有動畫,如果條件為真,則調用它。

它會是這樣的:

- (void)viewDidAppear:(BOOL)animated {

    [super viewDidAppear:animated];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];

    if(user.length>0){
        NSLog(@"Going straight to main %s", "Yes");
        [self performSegueWithIdentifier:@"startMain-noAnimation" sender:self];
      }
}

我想出了一種使用appdelegate和storyboard id來實現它的方法。 這就是我做的。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    //Check to see if this is a current user or a new user
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];

    //Current User
    if(user.length>0){
        self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];

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

        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"2"];

        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];

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

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

        UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"1"];

        self.window.rootViewController = viewController;
        [self.window makeKeyAndVisible];
    }


    return YES;
}

在你的appDelegate中試試這個

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

    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; NSString *user = [prefs stringForKey:@"userName"];
    if(user.length>0){
        NSLog(@"Going straight to main %s", "Yes");
        [self performSegueWithIdentifier:@"startMain" sender:self];

    } else {
        self.window.rootViewController = splashScreenViewController;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM