Reputation: 1214
Consider this view setup :
I have a view controller which switches between a set of sub views. Each sub view is a UIView subclass with custom code. To switch views I use a switch statement which allocs the new view as the currentview. This works very well.
I'm now in a position where I have a view (MainMenu) with a sub view (PopUp) that contains a UITableView. The PopUp view is shown and hidden via instance methods of the MainMenu.h class. Lets call the methods showPopUp and hidePopUp.
When a user selects an item from the UITableView they then have to manually close the containing (PopUp) view by clicking the close button, which is bound to the hidePopUp method.
What should happen when a user selects an item in the UITableView is that the hidePopUp method should be triggered automatically.
How do I trigger the hidePopUp instance method via the didSelectRowAtIndexPath of the UITAbleView? Is this a job for an app delegate, or perhaps NSNotificationCenter? I've tried such things as calling
[[[UIApplication sharedApplication] delegate] closePopUp];
from the didSelectRowAtIndexPath to no avail...
Thanks in advance, it's probably something simple I'm missing. Programming with a flu is difficult!
Upvotes: 3
Views: 653
Reputation: 2707
There are a few ways to accomplish this, such as notifications or working through a singleton like the app delegate (although the use of the singleton [anti]pattern is not without controversy). Personally, I'd use delegation.
Something like:
@protocol PopUpDelegate
@optional
- (void)Popup:(YourPopUpClass *)popUp didEndWithData:(NSData *)blah;
@end
You could then implement this protocol in your MainMenu, assign it as Popup's delegate, have the Popup call the delegate's method when the close button is pushed, and close the popup from there.
Here's a great post on how to implement delegates if you choose to go this route: How do I create delegates in Objective-C?
Upvotes: 1