我试图在当前视图控制器出现之前转到新的视图控制器,但似乎无法获得它。基本上我的应用程序,在启动页面上启动并检查是否有已保存的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];
}
}
我如何解决这个问题?
答案 0 :(得分:2)
为了实现你想要的,我。例如,根本没有看到第一个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];
}
}
答案 1 :(得分: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;
}
答案 2 :(得分:-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;
}
}