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

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

任何帮助表示赞赏!

mishajw126 asked 2020-07-19T10:16:33Z
11个解决方案
87 votes

如果您在导航控制器中:

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

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

ViewController *viewController = [[ViewController alloc] init];    
[self presentViewController:viewController animated:YES completion:nil];
SlateEntropy answered 2020-07-19T10:16:52Z
33 votes

如果要在同一情节提要中呈现新视图,

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

sree_achu answered 2020-07-19T10:17:21Z
18 votes

迅捷版:

如果您在导航控制器中:

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)
King-Wizard answered 2020-07-19T10:17:50Z
17 votes

instantiateViewControllerWithIdentifierStoryboard ID

NextViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextViewController"];
[self presentViewController:NVC animated:YES completion:nil];
Lukas Wiklund answered 2020-07-19T10:18:09Z
5 votes

取消由CmdSft用先前的答案代码调用的Viewcontroller

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

您可以使用

    [self dismissViewControllerAnimated:YES completion: nil];
BlueAMG63 answered 2020-07-19T10:18:34Z
4 votes

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)
Sahil answered 2020-07-19T10:19:02Z
3 votes
#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。 您可以在此处指定标识符。

NimzApps answered 2020-07-19T10:19:27Z
2 votes

这对我有用:

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];
}
Walter Schurter answered 2020-07-19T10:19:47Z
1 votes
[self.navigationController pushViewController:someViewController animated:YES];
StuR answered 2020-07-19T10:20:02Z
1 votes

雨燕3.0 in

一个视图控制器到secondviewcontroller去:

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

2nd viewcontroller到1stviewcontroller(后退)用于:动作事件后退按钮:-

self.navigationController?.popViewController(animated:true)

从第3个ViewController跳到第1个ViewController

self.navigationController?.popToRootViewController(animated:true)

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

ronak patel answered 2020-07-19T10:20:40Z
0 votes

我认为OP正在询问如何在不更改viewCONTROLLER的情况下交换VIEW。 我误会他的问题了吗?

他想用伪代码做到:

让myController =实例化(someParentController)

让view1 = Bundle.main.loadNib(....)为...等等myController.setThisViewTo(view1)

让view2 = Bundle.main.loadNib(....)为...等等myController.setThisViewTo(view2)

我把他的问题弄错了吗?

ChrisH answered 2020-07-19T10:21:23Z
translate from https://stackoverflow.com:/questions/10933939/how-can-i-switch-views-programmatically-in-a-view-controller-xcode-iphone