开发者

How to set a subview's delegate to the superviews controller

In IB I created UIView inside of a UIScrollView. File's owner for both is a UIViewController named JLViewController. The UIView is wired to class BodyClock.

The BodyClock class draws a graph inside the view. It also creates several small views which act as touch hot spots. When a hot spot is touched it displays an informative alert with a button for more detail. I need to tell JLViewController to display the detailed information. I thought I could do this by making the ViewController the HotSpot's delegate. Being as how I am creating the hot spots in the BodyClock class, I can't figure out how to set the hot spot delegate to JLViewController. I am trying to do somethin开发者_如何学Gog like this..

//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];
  }

Everything works except that respondsToSelector fails. If I NSLog viewController or self.delegate I get...

UIScrollView: 0x7443c20; frame etc...

I was expecting JLViewController: instead of UIScrollView: so I think this is the problem.

How do I set the delegate to the ViewController for these subviews?

Is it possible, or should I be using notification instead?


This:

id viewController = [self nextResponder];

isn't doing what you think it is doing. This doesn't get a view's controller but instead looks at the chain of responders and determines which is the next one for responding to events - these are typically views or controls (not to be confused with controllers). It appears that after your hot spot view, the scoll view is next.

You'll have to get the pointer to the controller correct. Maybe in your hot spot view you could add:

IBOutlet JLViewController *viewController;

And in interface builder connect this to your view controller.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜