在呈现View时导航栏没有显示?

时间:2016-10-05 16:59:40

标签: ios swift storyboard

在另一个文件中,我从另一个故事板创建了一个视图控制器实例,然后如下所示:

let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
self.presentViewController(viewController, animated: true, completion: nil)

在我的故事板中,我有一个正确的故事板ID,并在UINavigationController中嵌入了我的视图控制器。我还将初始视图控制器设置为UINavigationController。

为什么不出现?

3 个答案:

答案 0 :(得分:7)

创建视图控制器的对象,然后向其添加导航控制器,然后显示它:

let VC1 = self.storyboard!.instantiateViewControllerWithIdentifier("MyViewController") as! ViewController
let navController = UINavigationController(rootViewController: VC1) // Creating a navigation controller with VC1 at the root of the navigation stack.
self.presentViewController(navController, animated:true, completion: nil)

答案 1 :(得分:2)

好的,所以你要呈现视图控制器(请注意,它不是导航控制器)。

你能做什么:

  1. 推送视图控制器

    let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
    [self.navigationController pushViewController:viewController animated:YES];
    
  2. 介绍导航控制器

    let viewController:UIViewController = UIStoryboard(name: "UserProfile", bundle: nil).instantiateViewControllerWithIdentifier("profileID") as! ProfileViewController
    
    let navigationController = UINavigationController(rootViewController: viewController)
    
    self.presentViewController(viewController, animated: true, completion: nil)
    

答案 2 :(得分:0)

对于仍在使用Objective-C的用户:

UIStoryboard *sb = [UIStoryboard storyboardWithName: @"Storyboard" bundle: [NSBundle mainBundle]];
ViewController *vc = [sb instantiateInitialViewController];
UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController: vc];
[self presentViewController:nc animated:YES completion:nil];

别忘了加粗按钮:

enter image description here