6

我试图在当前视图控制器出现之前转到一个新的视图控制器,但似乎无法获得它。基本上我的应用程序从启动页面开始,并检查您是否保存了 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];

  }
}

我如何解决这个问题?

4

3 回答 3

2

为了达到你想要的,即根本看不到第一个viewController,你必须在transition中去掉动画。如果没有动画,用户将看到 secondViewController 是第一个呈现的。

您可以通过多种方式实现这一目标。其中一个(假设它是模态的)将在情节提要中创建一个新的转场,具有不同的标识符且没有动画,并在您的条件为真时调用它。

它会是这样的:

- (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];
      }
}
于 2013-09-02T02:54:31.760 回答
0

我想出了一种使用 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;
}
于 2013-09-02T16:34:49.420 回答
-3

在你的 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;
    }
}
于 2013-09-02T00:26:10.243 回答