如何在视图控制器中以编程方式切换视图? (Xcode,iPhone)

有一段时间一直在挣扎,似乎永远不能得到直接的答案。

任何帮助表示赞赏!

如果您在导航控制器中:

ViewController *viewController = [[ViewController alloc] init]; [self.navigationController pushViewController:viewController animated:YES]; 

或者如果你只是想提出一个新的观点:

 ViewController *viewController = [[ViewController alloc] init]; [self presentViewController:viewController animated:YES completion:nil]; 

如果你想在同一个故事板上呈现一个新的视图,

在CurrentViewController.m中,

 #import "YourViewController.h" UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; YourViewController *viewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"]; [self presentViewController:viewController animated:YES completion:nil]; 

要将标识符设置为视图控制器,请打开MainStoryBoard.storyboard。 selectYourViewController的View-> Utilities – > ShowIdentityInspector。 在那里你可以指定标识符。

instantiateViewControllerWithIdentifierStoryboard ID

 NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"]; [self presentViewController:NVC animated:YES completion:nil]; 

Swift版本:

如果您在导航控制器中:

 let viewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("VC") as ViewController self.navigationController?.pushViewController(viewController, animated: true) 

或者如果你只是想提出一个新的观点:

 let viewController: ViewController = self.storyboard?.instantiateViewControllerWithIdentifier("VC") as ViewController self.presentViewController(viewController, animated: true, completion: nil) 

通过CmdSftclosures以前的答案代码调用的Viewcontroller

  ViewController *viewController = [[ViewController alloc] init]; [self presentViewController:viewController animated:YES completion:nil]; 

您可以使用

  [self dismissViewControllerAnimated:YES completion: nil]; 
 #import "YourViewController.h" 

要推送包含导航栏和/或标签栏的视图:

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil]; YourViewController *viewController = (YourViewcontroller *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"]; [self.navigationController pushViewController:viewController animated:YES]; 

要将标识符设置为视图控制器,请打开YourStoryboard.storyboard。 selectYourViewController的View-> Utilities – > ShowIdentityInspector。 在那里你可以指定标识符。

这对我工作:

 NSTimer *switchTo = [NSTimer scheduledTimerWithTimeInterval:0.1 target:selfselector:@selector(switchToTimer)userInfo:nil repeats:NO]; - (void) switchToTimer { UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPad" bundle:nil]; UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyViewControllerID"]; // Storyboard ID [self presentViewController:vc animated:FALSE completion:nil]; } 
 [self.navigationController pushViewController:someViewController animated:YES]; 

Swift 3.0版本

如果你想呈现新的控制器。

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: "controllerIdentifier") as! YourController self.present(viewController, animated: true, completion: nil) 

如果你想推到另一个控制器(如果它在导航中)

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let viewController = storyboard.instantiateViewController(withIdentifier: "controllerIdentifier") as! YourController self.navigationController?.pushViewController(viewController, animated: true) 

Swift 3.0 in

一个视图控制器到secondviewcontroller去:

 let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MsgViewController") as! MsgViewController self.navigationController?.pushViewController(loginVC, animated: true) 

1stviewcontroller的第二个视图控制器(后退): 后退button的动作事件: –

 self.navigationController?.popViewController(animated:true) 

3rdviewcontroller到第一个viewcontroller跳转

 self.navigationController?.popToRootViewController(animated:true) 

和最重要的导航栏中的故事板unclicked确保比这回动作执行