Reputation: 5193
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?
Upvotes: 11
Views: 10838
Reputation: 9113
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];
Upvotes: 23
Reputation: 39
Here a very good example of how apply the delegation pattern on the iPhone. I downloaded the code an it works pretty good.
Upvotes: -1
Reputation: 33592
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+)
Upvotes: 0
Reputation: 70673
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.
Upvotes: -1
Reputation: 5219
It's a little hard to help you without any code given, but let's try:
Upvotes: 3