gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 11:27:08 27 4
gpt4 key购买 nike

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

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

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

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

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

Use of undeclared identifier ViewControllerMain.

在第二个中,它说:

unexpected interface name 'ViewController': expected identifier.

我能做什么?

最佳答案

创建 View Controller :

UIViewController * vc = [[UIViewController alloc] init];

调用 View Controller (必须从另一个 View Controller 中调用):

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

首先,使用 nil 而不是 null。


从 Storyboard 加载 View Controller :

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 你的 View Controller 或者等于你的 View Controller 的类名,或者你可以在你的 Storyboard的身份检查器中分配的 Storyboard ID。

关于ios - 如何以编程方式调用 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com