我有一个按钮动作,我试图在条件基础上打开视图控制器,但视图控制器没有打开。我使用故事板标识符方法打开新的视图控制器,我很困惑为什么它不打开。我的条件不正常,我得到了null但是当我登录并再次回到主屏幕时它再次显示为null。 我的代码是,
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"pin"];
if ([savedValue length]==0) {
NSLog(@"INNNNN");
UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: @"Register"];
[self.navigationController pushViewController:vc animated:YES];
}else{
UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: @"detail"];
[self.navigationController pushViewController:vc animated:YES];
}
}
答案 0 :(得分:1)
如果您的所有视图控制器未嵌入UINavigationcontroller(Root),则使用presentViewController
进行导航。
- (IBAction)History:(id)sender {
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:@"pin"];
UIStoryboard *story=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *topViewVC;
if ([savedValue length]==0) {
RegisterViewController *vc = [story instantiateViewControllerWithIdentifier: @"Register"];
topViewVC = vc;
}else{
DetailsViewController *vc = [story instantiateViewControllerWithIdentifier: @"detail"];
topViewVC = vc;
}
[self presentViewController:topViewVC animated:true completion:nil];
}
选项2
如果您希望pushViewController
可能是未来目的,则不要对代码进行任何更改,只需以root身份添加UINavigationcontroller
即可。在故事板中选择您的初始VC,然后选择xcode菜单 - >编辑 - - >嵌入 - >导航控制器
答案 1 :(得分:0)