如何从子视图访问UIViewcontrols方法?

发布于 2024-09-16 21:03:39 字数 266 浏览 4 评论 0原文

我每个选项卡中有 3 个视图控制器。 我想在每个视图控制器之上添加一个子视图。 子视图中有一个按钮(我做了一个带有按钮的UIView),

当我按下子视图中的按钮时,如何调用ViewController中的方法?

这是布局: ViewController1 有方法1 ViewController2有方法2 ViewController3有method3,

如果用户按下viewcontroller1中的按钮(子视图按钮),我如何调用method1? 有什么简单的例子吗?

i have 3 viewcontrollers in each tab.
i want to add a subview on top of each viewcontroller.
there is a button in subview (i made an UIView with a button)

when i press the button in subview, how to call a method in ViewController?

here is the layout:
ViewController1 has method1
ViewController2 has method2
ViewController3 has method3

if the user press the button (subviews button) in viewcontroller1, how can i call method1?
is there any simple example?

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

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

发布评论

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

评论(1

若水微香 2024-09-23 21:03:39
[yourButton addTarget:ViewController1 action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];

您应该在 UIView 继承类中保留 ViewController1 的引用。如果您没有 UIView 的类,并且您在 ViewController1 类中完成所有工作,则它将是:

[yourButton addTarget:self action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];
[yourButton addTarget:ViewController1 action:@selector(method1) forControlEvents:UIControlEventTouchUpInside];

You should keep a reference of the ViewController1 in the UIView inherited class. If you do not have a class for the UIView, and you do all the work in the ViewController1 class it would be:

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