已经为此苦苦挣扎了一段时间,似乎永远无法得到直接的答案。
任何帮助表示赞赏!
如果您在导航控制器中:
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。选择 YourViewController 视图-> 实用程序-> ShowIdentityInspector。您可以在此处指定标识符。
斯威夫特版本:
如果您在导航控制器中:
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)
instantiateViewControllerWithIdentifier
是Storyboard ID
。_
NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];
关闭 CmdSft 使用先前答案代码调用的 Viewcontroller
ViewController *viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
您可以使用
[self dismissViewControllerAnimated:YES completion: nil];
斯威夫特 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)
#import "YourViewController.h"
推送包含导航栏和/或标签栏的视图:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
YourViewController *viewController = (YourViewcontroller *)[storyboard instantiateViewControllerWithIdentifier:@"YourViewControllerIdentifier"];
[self.navigationController pushViewController:viewController animated:YES];
要将标识符设置为视图控制器,请打开 YourStoryboard.storyboard。选择 YourViewController 视图-> 实用程序-> 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];
斯威夫特 3.0 在
一个视图控制器到第二个视图控制器去:
let loginVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MsgViewController") as! MsgViewController
self.navigationController?.pushViewController(loginVC, animated: true)
2nd viewcontroller 到 1stviewcontroller(back)for:动作事件的后退按钮:-
self.navigationController?.popViewController(animated:true)
3rdviewcontroller 到 1st viewcontroller 跳转
self.navigationController?.popToRootViewController(animated:true)
并且未单击导航栏中最重要的情节提要确保执行此后退操作
我认为 OP 正在询问如何在不更改 viewCONTROLLER 的情况下交换 VIEW。我误解了他的问题吗?
在伪代码中,他想做:
let myController = instantiate(someParentController)
let view1 = Bundle.main.loadNib(....) as... blah
myController.setThisViewTo( view1 )
let view2 = Bundle.main.loadNib(....) as... blah
myController.setThisViewTo( view2 )
我是不是把他的问题弄错了?