从另一个ViewController呈现storyboard ViewController

时间:2012-01-19 09:58:02

标签: ios ios5 uiviewcontroller uistoryboard

我的iOS App的Storyboard中有多个UIViewControllers。我想从另一个UIViewController打开一个UIViewControllers(在故事板中)。

我已经尝试了下面的代码,但即使我在iOS 5之前使用过它也没有用,它运行正常。

- (IBAction)addNotes:(id)sender {
    NotesViewController *notesView = [[NotesViewController alloc] initWithNibName:@"NotesViewController" bundle:nil];
    [self presentModalViewController:notesView animated:YES];
}

如何使用iOS 5 Storyboards执行此操作?

6 个答案:

答案 0 :(得分:52)

假设您有故事板,请转到故事板并为您的VC提供标识符(检查员),然后执行:

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER"];
[self.navigationController pushViewController:vc animated:YES];

假设您有一个xib文件:

UIViewController *vc = [[UIViewController alloc] initWithNibName:@"NIBNAME" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];

没有xib文件:

UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];

答案 1 :(得分:6)

以下内容适用于 Swift 3.0 及以上版本。

  

故事板

let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "Identifier")
self.navigationController?.pushViewController(mainViewController, animated: true)
  

的.xib

    let viewController = UIViewController(nibName: "NibName", bundle: nil)
    self.navigationController?.pushViewController(viewController, animated: true)
  

没有.xib

let viewController = UIViewController()
self.navigationController?.pushViewController(viewController, animated: true)

答案 2 :(得分:2)

快捷键5

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ControllerIdentifier")
self.present(vc, animated: true, completion: nil)

答案 3 :(得分:1)

UIViewController *initialHelpView = [[UIStoryboard storyboardWithName:@"StoryBoard_IDentifier" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController_Identifier"];
[self presentViewController:initialHelpView animated:YES completion:nil];

答案 4 :(得分:1)

使用Swift 3.1版本进行更新

如果您没有嵌入导航控制器,那么<​​/ p>

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.present(viewController2, animated: true, completion: nil)

并且,如果您 嵌入了导航控制器,那么<​​/ p>

let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2")
self.navigationController?.pushViewController(viewController2, animated: true)

答案 5 :(得分:0)

在Swift 4.2答案中,对于那些希望在没有模式和快速更新版本导航的情况下获得答案的人。

let storyboard = UIStoryboard(name: "YourStoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "mainViewController")
self.present(controller, animated: true, completion: nil)