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

Sam*_*yes 67 objective-c uiviewcontroller ios uistoryboard presentviewcontroller

我已经查看了我可以在这个教程中找到的所有教程,但我仍然没有答案.我需要从代码中调用另一个视图.我在用UIStoryboards.我已经通过控制拖动多次更改了视图UIButtons,但现在它必须来自代码.如果是用户第一次打开应用程序,我试图从主菜单调用信息页面.但是,我似乎找不到从代码中更改视图的方法.我的所有视图都由相同的文件控制(ViewController2).在identifier我的主菜单是ViewControllerMain,而identifier该信息页面的是ViewControllerInfo.首先我尝试了这个:

[ViewControllerMain presentViewController: ViewControllerInfo 
                                 animated:YES 
                               completion: NULL];
Run Code Online (Sandbox Code Playgroud)

然后我尝试UIViewControllers为每个人做出不同的说法:

[ViewController2 presentViewController: ViewController 
                              animated:YES 
                            completion: NULL];
Run Code Online (Sandbox Code Playgroud)

都没有奏效.对于第一个,它说:

使用未声明的标识符ViewControllerMain.

在第二个中,它说:

意外的接口名称'ViewController':预期的标识符.

我能做什么?

190*_*Man 131

要创建视图控制器:

UIViewController * vc = [[UIViewController alloc] init];
Run Code Online (Sandbox Code Playgroud)

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

[self presentViewController:vc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

例如,使用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];
Run Code Online (Sandbox Code Playgroud)

Identifier 视图控制器的视图等于视图控制器的类名,或者您可以在故事板的标识检查器中分配的故事板ID.

  • 嗨@ 190290000卢布曼.这有效,但我想从故事板文件中显示已创建的视图.这只是给我一个空白的视图.有没有办法做到这一点?我正竭尽全力.我当然有很多需要学习的东西. (2认同)
  • 谢谢@ 190290000卢布曼!你最近的编辑工作了! (2认同)

tig*_*loo 20

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

ViewControllerInfo* infoController = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerInfo"];
[self.navigationController pushViewController:infoController animated:YES];
Run Code Online (Sandbox Code Playgroud)

此示例假定您具有导航控制器以返回上一个视图.你当然也可以使用presentViewController:animated:completion:.重点是让故事板使用目标视图控制器的ID实例化目标视图控制器.


Sur*_*gch 20

迅速

这将从故事板中获取视图控制器并呈现它.

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

根据需要更改故事板名称,视图控制器名称和视图控制器ID.


swi*_*Boy 5

您可以通过这种方式调用ViewController,如果您想使用NavigationController

在此输入图像描述

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

VerifyExpViewController *addProjectViewController = [[VerifyExpViewController alloc] init];
[self.navigationController pushViewController:addProjectViewController animated:YES];
Run Code Online (Sandbox Code Playgroud)

2.1在加载视图中:在.h文件中添加以下内容

@interface VerifyExpViewController : UIViewController <UINavigationControllerDelegate>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

希望这对那里的人有用:)