告诉 UIViewController 子视图中发生的事情
我有一个 UIViewController
,其中有一个 UIView
作为子视图。我希望 UIViewController
能够收到(或监听)UIView
中某些操作的通知。 执行此操作的标准方法是什么?
I have a UIViewController
with a UIView
as a subview. I'd like the UIViewController
to be notified of (or listen for) certain actions in the UIView
. What's the standard way of doing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想有一个操作处理程序:
有关详细信息,请参阅
UIControl
文档:或者,如果您正在谈论调用父视图控制器的视图控制器,您可以使用
UIViewController
访问“父”视图 控制器:或者
UIView
:If you just want to have an action handler:
For more info, see the
UIControl
Documentation:Or, if your talking about a view controller calling a parent view controller, you could use the
presentingViewController
property of aUIViewController
to access the "parent" view controller:Or the
superview
property of aUIView
:根据您对上面评论的回答,这是一个按钮按下,“标准”方法是使用 UIControl 中的机制。因此,以编程方式,如果您
按“myButton”,将导致“buttonPressed:”方法在 myViewController 上执行。您还可以使用 Interface Builder 来建立连接。
这段代码可以放在许多不同的地方(在 UIView 中,在 UIViewController 中),因此,变量的确切名称会改变,但是,对于处理按下的按钮,这可能是最简单的方法。
Based on your answer to the comment above, that this is a button press, the "standard" way would be to use the mechanisms in UIControl. So, programmatically, if you do
Pressing "myButton" will cause the "buttonPressed:" method to be executed on myViewController. You can also use Interface Builder to make the connections.
This code could be put in a number of different places (in the UIView, in the UIViewController) and, as a result, the exact name of the variables would change, but, for handling a button pressed, this is probably the easiest way.