44

已经为此苦苦挣扎了一段时间,似乎永远无法得到直接的答案。

任何帮助表示赞赏!

4

11 回答 11

87

如果您在导航控制器中:

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

或者,如果您只想呈现一个新视图:

ViewController *viewController = [[ViewController alloc] init];    
[self presentViewController:viewController animated:YES completion:nil];
于 2012-06-07T14:54:20.980 回答
33

如果您想在同一个故事板中呈现新视图,

在 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。您可以在此处指定标识符。

于 2013-04-25T09:25:44.220 回答
18

斯威夫特版本:

如果您在导航控制器中:

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)
于 2014-12-15T01:13:41.527 回答
17

instantiateViewControllerWithIdentifierStoryboard ID。_

NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];
于 2013-05-02T14:56:38.227 回答
5

关闭 CmdSft 使用先前答案代码调用的 Viewcontroller

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

您可以使用

[self dismissViewControllerAnimated:YES completion: nil];
于 2014-04-15T20:00:41.393 回答
4

斯威夫特 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)
于 2017-02-10T06:36:38.377 回答
3
#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。您可以在此处指定标识符。

于 2014-10-22T11:44:57.877 回答
2

这对我有用:

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];
}
于 2013-12-02T10:16:21.380 回答
1
[self.navigationController pushViewController:someViewController animated:YES];
于 2012-06-07T14:40:27.200 回答
1

斯威夫特 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)

并且未单击导航栏中最重要的情节提要确保执行此后退操作

于 2017-07-22T09:45:06.400 回答
0

我认为 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 )

我是不是把他的问题弄错了?

于 2018-09-22T01:07:17.307 回答