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

时间:2012-06-07 14:34:11

标签: swift uiviewcontroller swift3 storyboard

一直在努力解决这个问题,似乎永远无法得到直接答案。

感谢任何帮助!

11 个答案:

答案 0 :(得分:87)

如果您在导航控制器中:

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

或者您只想提出一个新观点:

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

答案 1 :(得分: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。 在那里你可以指定标识符。

答案 2 :(得分:17)

instantiateViewControllerWithIdentifierStoryboard ID

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

答案 3 :(得分:17)

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)

答案 4 :(得分:5)

通过CmdSft

关闭使用之前的答案代码调用的Viewcontroller
    ViewController *viewController = [[ViewController alloc] init];    
    [self presentViewController:viewController animated:YES completion:nil];

你可以使用

    [self dismissViewControllerAnimated:YES completion: nil];

答案 5 :(得分:4)

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)

答案 6 :(得分: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。在那里你可以指定标识符。

答案 7 :(得分: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];
}

答案 8 :(得分:1)

[self.navigationController pushViewController:someViewController animated:YES];

答案 9 :(得分:1)

Swift 3.0 in

一个视图控制器到secondviewcontroller go:

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

第二个viewcontroller到1stviewcontroller(后退):动作事件的后退按钮: -

self.navigationController?.popViewController(animated:true)

3rdviewcontroller到1st viewcontroller跳转

self.navigationController?.popToRootViewController(animated:true)

导航栏中最重要的故事板未点击确保比此后退动作执行

答案 10 :(得分:0)

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

他想用伪代码做到:

让myController =实例化(someParentController)

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

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

我把他的问题弄错了吗?