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

2020/11/25 21:51 · ios ·  · 0评论

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

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

然后,我尝试使UIViewControllers每种方法有所不同并说:

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

两者都不起作用。对于第一个,它说:

使用未声明的标识符ViewControllerMain。

在第二个中,它说:

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

我能做什么?

创建视图控制器:

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实例化目标视图控制器。

迅速

这将从情节提要中获取视图控制器并进行呈现。

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

适当更改情节提要板名称,视图控制器名称和视图控制器ID。

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

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

有两种方法可以执行此操作:

1,按照我的答案在此处为故事板中的ViewController创建序列:如何执行与iOS 5中用户输入无关的序列?

2,提供您的ViewController和标识符,并使用我在此处答案中的代码对其进行调用:以编程方式调用情节提要场景(无需继续)?

is_背后的主要逻辑

NSString * storyboardIdentifier = @"SecondStoryBoard";

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

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

[self presentViewController:UIVC 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];

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

KartViewController *viewKart = [[KartViewController alloc]initWithNibName:@"KartViewController" bundle:nil];
[self presentViewController:viewKart animated:YES completion:nil];
本文地址:http://ios.askforanswer.com/ruheyibianchengfangshidiaoyongview-controller.html
文章标签: ,   ,   ,   ,  
版权声明:本文为原创文章,版权归 admin 所有,欢迎分享本文,转载请保留出处!

文件下载

老薛主机终身7折优惠码boke112

上一篇:
下一篇:

评论已关闭!