iPhone:从子视图调用父/超级方法
希望有人能帮助我解决这个问题,因为我已经被困了几个小时了。
我正在尝试制作一种图画书。 我有一个视图,它是我的容器,我通过使用 addsubview 添加子视图。
在子视图上,我有滑动手势等,我想在父视图中触发方法。我弄清楚了如何触发委托,但无法让委托触发父视图。我读过 10 多种不同的方法,但没有一个有效。
我现在很困惑超级视图是什么。只是为了混淆问题,委托有一个选项卡控制器,父视图是选项卡按钮 1
我尝试过
[self.view.superview method]
[self.superview method]
在委托上我尝试过 self.tabcontroller.parentviewcontroller,selectedview,super view.super
更新: 子视图需要独立于父视图,因为它是可重用的视图。 另外,我还没有将父视图设置为超级视图,因为我只是认为超级视图是带有子视图的视图(请不要杀我)。所以也许我只需要将父视图设置为超级视图?
hope someone can help me on this as been stuck for hours.
I am trying to make a kind of picture book.
I have a view which is my container and I add subviews to that by using addsubview.
On the subview, I have swipe gestures etc that I want to trigger off method in the parent view. I worked out how to trigger the delegate but I cant get the delegate to trigger the parent view. I have read over 10 different ways of doing it and none work.
I now very confused about what a super view is to. Just to confuse matters, the delegate has a tabcontroller and the parent view is tab button 1
I tried
[self.view.superview method]
[self.superview method]
On the delegate I tried
self.tabcontroller.parentviewcontroller, selectedview, super view.super
UPDATE :
The subview needs to be independant of the parent view as its a reusable view.
Also I have not set the parentview to superview as I just thought a superview is a view with subviews (please don't kill me). So maybe I just need to set the parentview to a superview?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
做这些事情的正确方法是使用协议和委托模式。
定义一个协议,
然后在你的超级视图中实现该协议:
在你的子视图中定义一个委托属性,就像这样
在你的子视图中,像这样调用委托
The proper way of doing such things is to use protocol and delegate pattern.
Define a protocol like
then implement that protocol in your superview :
define a delegate property in your SubView like this
the in your subview, call the delegate like this
如果没有给出任何代码,要帮助您有点困难,但让我们尝试:
It's a little hard to help you without any code given, but let's try:
对于自定义视图,您可以子类化 UIControl 并使用控件事件:
UIControlEventApplicationReserved = 0x0F000000
)[self sendActionsForControlEvents:events]
或者,您可以使用 UIGestureRecognizer 风格的界面 (addTarget:action:)。
或者只使用 UIGestureRecognizer (OS 3.2+)
For a custom view, you could subclass UIControl and use control events:
UIControlEventApplicationReserved = 0x0F000000
)[self sendActionsForControlEvents:events]
Alternatively, you could use a UIGestureRecognizer-style interface (addTarget:action:).
Alternatively just use UIGestureRecognizer (OS 3.2+)
您的父视图在添加子视图时是否将自己设置为子视图的超级视图?否则子视图不知道它的超级视图是谁。
更标准的命名方法是调用委托而不是超级视图的方法处理程序,使其成为一个属性,并让子视图检查所设置的委托是否存在以及它是否可以处理该方法。
Did your parent view set itself to be the superview of the subview when it added the subview? Otherwise the subview doesn't know who its superview is.
The more standard way of naming things to call the method handler the delegate instead of the superview, make it a property, and have the subview check for both the existence of the delegate being set and whether it can handle the method.
这是一个很好的例子,展示了如何在 iPhone 上应用委托模式。我下载了代码,效果很好。
http://www.hivestudio.cat/index.php?option=com_content&view=article&id=57:technical-note-the-delegation -pattern-on-the-iphone&catid=35:technical-note-category&Itemid=76
Here a very good example of how apply the delegation pattern on the iPhone. I downloaded the code an it works pretty good.
http://www.hivestudio.cat/index.php?option=com_content&view=article&id=57:technical-note-the-delegation-pattern-on-the-iphone&catid=35:technical-note-category&Itemid=76