单击按钮上的新视图控制器单击IOS

时间:2017-09-22 11:45:39

标签: ios objective-c

我有一个按钮动作,我试图在条件基础上打开视图控制器,但视图控制器没有打开。我使用故事板标识符方法打开新的视图控制器,我很困惑为什么它不打开。我的条件不正常,我得到了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];
}

}

故事板看起来像这样, enter image description here

2 个答案:

答案 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)

好像你错过了将基本视图控制器嵌入到导航控制器中。在故事板中选择您的初始VC。转到编辑 - >嵌入 - >导航控制器。 enter image description here