如何以编程方式调用View Controller?

我已经看过了我能find的所有教程,但我仍然没有答案。 我需要从代码调用另一个视图。 我正在使用UIStoryboards 。 我已经通过从UIButtons拖拽来多次改变视图,但是现在它必须来自代码。 我试图从主菜单调用信息页,如果这是用户第一次打开应用程序。 但是,我似乎无法find从代码中更改视图的方法。 我的所有视图都由相同的文件( ViewController2 )控制。 我的主菜单的identifierViewControllerMain ,信息页面的identifierViewControllerInfo 。 首先我试过这个:

 [ViewControllerMain presentViewController: ViewControllerInfo animated:YES completion: NULL]; 

然后我试着为每个UIViewControllers做不同的UIViewControllers ,并说:

 [ViewController2 presentViewController: ViewController animated:YES completion: NULL]; 

都没有工作。 对于第一个,它说:

使用未声明的标识符ViewControllerMain。

在第二个,它说:

意外的接口名称“ViewController”:预期的标识符。

我能做什么?

要创build一个视图控制器:

 UIViewController * vc = [[UIViewController alloc] init]; 

调用视图控制器(必须从另一个视图控制器中调用):

 [self presentViewController:vc animated:YES completion:nil]; 

首先,使用nil而不是null。


从故事板加载视图控制器:

 NSString * storyboardName = @"MainStoryboard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil]; UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"]; [self presentViewController:vc animated:YES completion:nil]; 

视图控制器的Identifier等于视图控制器的类名,或者可以在故事板的身份检查器中分配的故事板ID。

您需要从故事板实例化视图控制器,然后显示它:

 ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"]; [self.navigationController pushViewController:infoController animated:YES]; 

这个例子假定你有一个导航控制器为了返回到前一个视图。 你当然也可以使用presentViewController:animated:completion :. 主要的是让你的故事板使用目标视图控制器的ID实例化你的目标视图控制器。

迅速

更新了Swift 3.0

这从故事板获得一个视图控制器并呈现它。

 let storyboard = UIStoryboard(name: "Main", bundle: nil) let secondViewController = storyboard.instantiateViewController(withIdentifier: "secondViewControllerId") as! SecondViewController self.present(secondViewController, animated: true, completion: nil) 

根据情况更改故事板名称,视图控制器名称和视图控制器ID。

有两种方法可以做到这一点:

1,在你的Storyboard中为你的ViewController创build一个segue,正如我在这里回答的: 在iOS 5中如何执行与用户input无关的segue?

2,给你的ViewController和标识符,并使用我的答案在这里的代码调用它:以编程方式调用故事板场景(不需要segue)?

你可以这样调用ViewController,如果你想用NavigationController的话

在这里输入图像说明

1.在当前屏幕:加载新屏幕

 VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init]; [self.navigationController pushViewController:addProjectViewController animated:YES]; 

2.1在加载的视图:在下面添加.h文件

 @interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate> 

2.2在加载视图:在.m文件中添加如下

  @implementation VerifyExpViewController - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.delegate = self; [self setNavigationBar]; } -(void)setNavigationBar { self.navigationController.navigationBar.backgroundColor = [UIColor clearColor]; self.navigationController.navigationBar.translucent = YES; [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"B_topbar.png"] forBarMetrics:UIBarMetricsDefault]; self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]}; self.navigationItem.hidesBackButton = YES; self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Btn_topback.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onBackButtonTap:)]; self.navigationItem.leftBarButtonItem.tintColor = [UIColor lightGrayColor]; self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Save.png"] style:UIBarButtonItemStylePlain target:self action:@selector(onSaveButtonTap:)]; self.navigationItem.rightBarButtonItem.tintColor = [UIColor lightGrayColor]; } -(void)onBackButtonTap:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } -(IBAction)onSaveButtonTap:(id)sender { //todo for save button } @end 

希望这将是有用的:)

这背后的主要逻辑是,

 NSString * storyboardIdentifier = @"SecondStoryBoard"; UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil]; UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"]; [self presentViewController:UIVC animated:YES completion:nil]; 

导入您想要显示的视图控制器类,并使用下面的代码

 KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil]; [self presentViewController:viewKart animated:YES completion:nil]; 
  UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard_iPhone_iOS7" bundle:nil]; AccountViewController * controller = [storyboard instantiateViewControllerWithIdentifier:@"accountView"]; // [self presentViewController:controller animated:YES completion:nil]; UIViewController *topRootViewController = [UIApplication sharedApplication].keyWindow.rootViewController; while (topRootViewController.presentedViewController) { topRootViewController = topRootViewController.presentedViewController; } [topRootViewController presentViewController:controller animated:YES completion:nil];