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

时间:2013-04-21 17:55:12

标签: ios objective-c uiviewcontroller uistoryboard presentviewcontroller

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

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

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

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

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

  

使用未声明的标识符ViewControllerMain。

在第二个中,它说:

  

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

我该怎么办?

7 个答案:

答案 0 :(得分: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。

答案 1 :(得分:20)

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

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

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

答案 2 :(得分:19)

夫特

这会从故事板中获取一个视图控制器并显示它。

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

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

答案 3 :(得分:5)

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

enter image description here

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

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

答案 4 :(得分:3)

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

1,按照我在这里的回答中的解释,在故事板中为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)?

答案 5 :(得分:1)

这背后的主要逻辑是_,

NSString * storyboardIdentifier = @"SecondStoryBoard";

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

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

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

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