iPhone:从子视图调用父/超级方法

发布于 2024-09-24 23:01:00 字数 516 浏览 15 评论 0原文

希望有人能帮助我解决这个问题,因为我已经被困了几个小时了。

我正在尝试制作一种图画书。 我有一个视图,它是我的容器,我通过使用 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 技术交流群。

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

发布评论

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

评论(5

心在旅行 2024-10-01 23:01:00

做这些事情的正确方法是使用协议和委托模式。

定义一个协议,

@protocol subViewDelegate
   -(void)somethingHappened:(id)sender;
@end

然后在你的超级视图中实现该协议:

@interface superView:UIViewController<subViewDelegate> {
...
}
...
@end

在你的子视图中定义一个委托属性,就像这样

@interface subView : UIView {
   id<subViewDelegate> delegate;
   ...
}
@propery (nonatomic, assign) id<subViewDelegate> delegate;
...
@end

在你的子视图中,像这样调用委托

[self.delegate somethingHappened :self];

The proper way of doing such things is to use protocol and delegate pattern.

Define a protocol like

@protocol subViewDelegate
   -(void)somethingHappened:(id)sender;
@end

then implement that protocol in your superview :

@interface superView:UIViewController<subViewDelegate> {
...
}
...
@end

define a delegate property in your SubView like this

@interface subView : UIView {
   id<subViewDelegate> delegate;
   ...
}
@propery (nonatomic, assign) id<subViewDelegate> delegate;
...
@end

the in your subview, call the delegate like this

[self.delegate somethingHappened :self];
无所谓啦 2024-10-01 23:01:00

如果没有给出任何代码,要帮助您有点困难,但让我们尝试:

  1. 创建一个协议:按照您喜欢的方式命名它(我将其称为“MyProtocol”)并向其添加您要在超级视图中调用的函数的定义,让我们称之为“respondToSwipe”
  2. 如果您的超级视图是 UIView,您必须创建自己的 UIView 子类并使您的超级视图成为该类的实例。
  3. 让您(新)创建的超级视图类实现 1.) 的协议并实现“respondToSwipe”方法
  4. 在您的子视图中创建类型 id 的实例变量,并根据您的喜好命名它,例如“myDelegate”。
  5. 将 2/3 中创建的超级视图传递给您的“myDelegate”变量
  6. 随时调用 [myDelegate respondToSwipe]

It's a little hard to help you without any code given, but let's try:

  1. Create a protocol: Name it however you like (I will call it "MyProtocol") and add to it the definition of the function you want to call in your superview, let's call it "respondToSwipe"
  2. If your superview is a UIView, you have to create your own subclass of UIView and make your superview an instance of that class.
  3. Let your (newly) created superview class implement the protocol of 1.) an implement the "respondToSwipe" method
  4. Create an instance variable of the the type id in your subview, and name it however you like, e.g. "myDelegate".
  5. Pass the superview created in 2/3.) to your "myDelegate" variable
  6. Call [myDelegate respondToSwipe] whenever you like
海螺姑娘 2024-10-01 23:01:00

对于自定义视图,您可以子类化 UIControl 并使用控件事件:

  • 定义一些控件事件。您可以随意编写 4 个控件事件 (UIControlEventApplicationReserved = 0x0F000000)
  • 让想要接收事件的人调用 addTarget:action:forControlEvents:
  • 让控件调用 [self sendActionsForControlEvents:events]

或者,您可以使用 UIGestureRecognizer 风格的界面 (addTarget:action:)。

或者只使用 UIGestureRecognizer (OS 3.2+)

For a custom view, you could subclass UIControl and use control events:

  • Define some control events. You're free to make up 4 control events (UIControlEventApplicationReserved = 0x0F000000)
  • Have whoever wants to receive events call addTarget:action:forControlEvents:
  • Have the control call [self sendActionsForControlEvents:events]

Alternatively, you could use a UIGestureRecognizer-style interface (addTarget:action:).

Alternatively just use UIGestureRecognizer (OS 3.2+)

皇甫轩 2024-10-01 23:01:00

您的父视图在添加子视图时是否将自己设置为子视图的超级视图?否则子视图不知道它的超级视图是谁。

更标准的命名方法是调用委托而不是超级视图的方法处理程序,使其成为一个属性,并让子视图检查所设置的委托是否存在以及它是否可以处理该方法。

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.

凉风有信 2024-10-01 23:01:00

这是一个很好的例子,展示了如何在 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

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