从另一个ViewController演示故事板ViewController
我的iOS应用程序的Storyboard中有多个UIViewController。 我想从一个不同的UIViewController中打开一个UIViewControllers(在storyboard中)。
我试过下面的代码,但它不工作,即使我在iOS 5之前使用它,它工作正常。
- (IBAction)addNotes:(id)sender { NotesViewController *notesView = [[NotesViewController alloc] initWithNibName:@"NotesViewController" bundle:nil]; [self presentModalViewController:notesView animated:YES]; }
如何使用iOS 5 Storyboard执行此操作?
假设你有故事板,去故事板并给你一个标识符(检查员),然后做:
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];
这是Swift 3版本
故事板
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)
UIViewController *initialHelpView = [[UIStoryboard storyboardWithName:@"StoryBoard_IDentifier" bundle:nil] instantiateViewControllerWithIdentifier:@"ViewController_Identifier"]; [self presentViewController:initialHelpView animated:YES completion:nil];
用Swift 3.1版本更新
如果你还没有embedded导航控制器
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2") self.present(viewController2, animated: true, completion: nil)
如果你已经embedded了导航控制器
let storyBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil) let viewController2 = storyBoard.instantiateViewController(withIdentifier: "ViewController2") self.navigationController?.pushViewController(viewController2, animated: true)