欢迎访问宙启技术站
智能推送

UIViewController怎么调用

发布时间:2023-05-17 06:24:21

UIViewController是iOS中的一个基础控制器,用来管理APP中的View和控制器之间的交互与数据传递。在使用UIViewController时,需要进行创建和调用,下面我们就来详细介绍如何调用UIViewController。

1.创建UIViewController

在调用UIViewController之前,首先需要进行创建,创建方式可以通过代码或Storyboard来实现。

通过代码创建UIViewController:

UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];

通过Storyboard创建UIViewController:

1)在Storyboard中选择需要创建的UIViewController,并设置其Storyboard ID,例如“ViewControllerID”。

2)通过以下方式创建UIViewController:

UIViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewControllerID"];
[self presentViewController:vc animated:YES completion:nil];

2.调用UIViewController

UIViewController可以通过push、present和addSubview三种方式进行调用。

通过push方式调用:

UIViewController *vc = [[UIViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];

通过present方式调用:

UIViewController *vc = [[UIViewController alloc] init];
[self presentViewController:vc animated:YES completion:nil];

通过addSubview方式调用:

UIViewController *vc = [[UIViewController alloc] init];
[self.view addSubview:vc.view];
[self addChildViewController:vc];

注意:

1)push调用的UIViewController需要在UINavigationController的栈中;

2)present调用的UIViewController需要在当前UIViewController的presentingViewController中;

3)通过addSubview添加的UIViewController需要调用addChildViewController方法。

综上所述,调用UIViewController需要先进行创建,然后通过push、present和addSubview三种方式进行调用。根据不同的场景,可以选择合适的调用方式,并注意调用的UIViewController需要在相应的位置。