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

问题描述 投票:63回答:8

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

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

然后我尝试为每个人制作不同的UIViewControllers并说:

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

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

使用未声明的标识符ViewControllerMain。

在第二个中,它说:

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

我能做什么?

ios objective-c uiviewcontroller uistoryboard presentviewcontroller
8个回答
130
投票

要创建视图控制器:

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。


20
投票

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

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

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


19
投票

Swift

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

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

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


5
投票

您可以通过这种方式调用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

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


3
投票

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

1,在我的答案中解释为你的Storyboard中的ViewController创建一个segue:How to perform a segue that is not related to user input in iOS 5?

2,给你的ViewController和标识符,并使用我的答案中的代码调用它:Call storyboard scene programmatically (without needing segue)?


1
投票

这背后的主要逻辑是_,

NSString * storyboardIdentifier = @"SecondStoryBoard";

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardIdentifier bundle: nil];

UIViewController * UIVC = [storyboard instantiateViewControllerWithIdentifier:@"YourviewControllerIdentifer"];

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

0
投票

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

KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];

0
投票
        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];
© www.soinside.com 2019 - 2024. All rights reserved.