【问题标题】:How to set a subview's delegate to the superviews controller如何将子视图的委托设置为超级视图控制器
【发布时间】:2011-05-06 11:11:34
【问题描述】:

在 IB 中,我在 UIScrollView 中创建了 UIView。两者的文件所有者都是名为 JLViewController 的 UIViewController。 UIView 连接到 BodyClock 类。

BodyClock 类在视图内绘制图形。它还创建了几个充当触摸热点的小视图。当一个热点被触摸时,它会显示一个信息警报,并带有一个按钮以获取更多详细信息。我需要告诉 JLViewController 显示详细信息。我想我可以通过让 ViewController 成为 HotSpot 的代表来做到这一点。作为我在 BodyClock 类中创建热点的方式,我无法弄清楚如何将热点委托设置为 JLViewController。我正在尝试做这样的事情..

//Code in BodyClock
//create the hot spot
  id viewController = [self nextResponder];
  HelpHotSpot *helpHotSpot = [[HelpHotSpot alloc] initWithFrame:CGRectMake(start_x, melatoninHeightEnd_y, 80, 40)]; 
  helpHotSpot.delegate = viewController;
  [viewController addSubview:helpHotSpot];
  [helpHotSpot release];

//Code in the HotSpot after touch and request for more info
//notify JLViewController to display the details
  if ([self.delegate respondsToSelector:@selector(hotSpotMore:)]) {
     [self.delegate hotSpotMore:itemDetails];
  }

除了 respondsToSelector 失败之外,一切正常。如果我 NSLog viewController 或 self.delegate 我得到...

UIScrollView: 0x7443c20;框架等...

我期待 JLViewController: 而不是 UIScrollView: 所以我认为这是问题所在。

如何将代理设置为这些子视图的 ViewController?

有可能吗,还是我应该改用通知?

【问题讨论】:

  • 我仍然对如何执行此操作感兴趣,但已经通过使用 NSNotificationCenter 解决了我的问题。非常容易实现并且效果很好。

标签: iphone xcode delegates uiviewcontroller subview


【解决方案1】:

这个:

id viewController = [self nextResponder];

没有做你认为它正在做的事情。这不会获取视图的控制器,而是查看响应者链并确定下一个响应事件的响应者 - 这些通常是视图或控件(不要与控制器混淆)。看来,在您的热点视图之后,接下来是 scoll 视图。

您必须正确获取指向控制器的指针。也许在您的热点视图中您可以添加:

IBOutlet JLViewController *viewController;

然后在界面生成器中将其连接到您的视图控制器。

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多