xcode 4.2 故事板标识符

发布于 2024-12-29 13:47:05 字数 226 浏览 4 评论 0原文

我正在编写一个应用程序,其中通过代码向我的视图(mainView)添加一些按钮。当用户点击此按钮时,我希望显示一个新视图(resultView)。

我的故事板中有这两个视图,但由于故事板中的 mainView 上没有按钮,我无法使用故事板将 mainView 和 resultView 相互链接。一切都必须通过代码来完成。

我想知道如何通过代码链接我的两个视图并为该链接定义标识符。

谢谢,

I'm writing an app in which I add some buttons to my view (mainView) by code. When the user taps on this buttons I want a new view (resultView) to show.

I have both of this views in my storyboard, but since there is no button on my mainView that is in storyboard I can't link the mainView and resultView to each other using storyboard. Everything must be done by code.

I want to know how I can link my two views by code and define an identifier for that link.

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生共白头 2025-01-05 13:47:05

要以编程方式切换视图控制器,您必须在“视图控制器”部分的属性检查器中设置标识符。之后,您可以在代码中使用该值。这里我使用了标识符“Menu”。

UIStoryboard *storyboard = self.storyboard;
MenuTableViewController *mtvc = [storyboard instantiateViewControllerWithIdentifier:@"Menu"];

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

文档里也有说明,随便找一下

以编程方式实例化 Storyboard 的视图控制器

To switch the view controller programmatically, you have to set a identifier in the attribute inspector at section "View Controller". After that, you can just use the value inside your code. Here i used the identifier "Menu".

UIStoryboard *storyboard = self.storyboard;
MenuTableViewController *mtvc = [storyboard instantiateViewControllerWithIdentifier:@"Menu"];

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

There is also a description in the documentation, just look for

Instantiating a Storyboard’s View Controller Programmatically

美羊羊 2025-01-05 13:47:05

在 IB 的属性检查器中为两者之间的 Segue 指定一个标识符,然后在按钮按下事件上调用 [self PerformSegueWithIdentifier:@"MySegueIdentifier" sender:theButton]

Give the segue between the two an identifier in the Attribute Inspector in IB, then on your button press event call [self performSegueWithIdentifier:@"MySegueIdentifier" sender:theButton]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文