11

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?

Burf2000
  • 5,001
  • 14
  • 58
  • 117

5 Answers5

23

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];
Demian Turner
  • 436
  • 3
  • 12
VdesmedT
  • 9,037
  • 3
  • 34
  • 50
  • These are viewControllers not views. Also the subview can be independent of the super view. E.g its used all other the place – Burf2000 Sep 22 '10 at 15:58
  • same principle apply to both. I assumed that you has one viewController managing a view and its subviews. – VdesmedT Sep 22 '10 at 16:06
  • Nope, each one has its one viewController. So each page of a book with be a separate class – Burf2000 Sep 22 '10 at 16:08
  • I had to put the protocol in to the appdelegate and i get 2010-09-22 17:13:12.805 UniversalApp[27372:207] -[CustomViewController delegate]: unrecognized selector sent to instance 0x7b36bd0 2010-09-22 17:13:12.808 UniversalApp[27372:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CustomViewController delegate]: unrecognized selector sent to instance 0x7b36bd0' – Burf2000 Sep 22 '10 at 16:15
  • I know this is old, but I wanted to add that in your superView implementation after you create your subview you need to set its delegate like so: `subView.delegate = self;` – tptcat Aug 29 '13 at 13:41
3

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
Philipp Schlösser
  • 5,179
  • 2
  • 38
  • 52
0

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+)

tc.
  • 33,468
  • 5
  • 78
  • 96
-1

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.

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • I not set anything to superview, I assumed it was super as I added a view to it – Burf2000 Sep 22 '10 at 16:02
  • "super" is not the same as superview. So your assumption is incorrect for multiple reasons (which is a different question). – hotpaw2 Sep 22 '10 at 16:49
  • I add a subview to a view, then I want to call the parent view methods? any ideas? – Burf2000 Sep 22 '10 at 16:50
  • Add a delegate to your subview, and set it from your parent view. The delegate will be what you want to call a "superview". There's nothing automatic (and simple), you have to do it manually and explicitly. – hotpaw2 Sep 22 '10 at 17:20
  • The superview pointer is automatically handled for you by UIView. – tc. Sep 23 '10 at 00:34
-1

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

user497293
  • 39
  • 1