akhalsa
akhalsa

Reputation: 2313

iOS popovers as menu tutorial

I am writing an iPad app and am very new to objective C. I am currently trying to use a popover as a menu within a view controller. I understand how to set the popup up and have a tableview in it for a user to select an item from. However, I am not sure how to then pass the information back to the container view which holds the popover. didSelectRowAtIndexPath will be called within the UITableViewController which is presumably inside the popover view controller. Can anyone point me in the right direction for how to get this information back to the container?

Thanks in advance!

Upvotes: 1

Views: 3381

Answers (3)

alvira
alvira

Reputation: 67

I'm also looking for the answer to this problem. I did solve it using NSNotification.

Here's how:

The View Controller that displays the popup should register to receive the notification:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(languageSetup) name:SPVWChangeLanguage object:nil];

// languageSetup is the function that will be called, SPVWChangeLanguage is a string that you will define in your popup view controller

I added this line in viewDidAppear

Now in your popup view controller add this line when the user selects something in the table:

[[NSNotificationCenter defaultCenter] postNotification: [NSNotification notificationWithName: SPVWChangeLanguage object:self]];

before dismissing the popup:

[self.popOverController dismissPopoverAnimated:YES];

That's all. The selector languageSetup (in my case) will be called. Be sure to remove the notification when your view controller goes away:

[[NSNotificationCenter defaultCenter] removeObserver:self];

I do it in viewWillDisappear

I think there must be an easier way. But at least, this one works for me.

Upvotes: 0

El Developer
El Developer

Reputation: 3346

Well, I assume that the delegate of your UIPopoverController is your content view, if so, you can request the parentViewController property from the UITableView and then cast it to a UIPopoverController to get it's delegate (in your case the delegate would be the container view).

Upvotes: 0

user523234
user523234

Reputation: 14834

You need to use delegate. It is very commonly used pattern in this situation. I have an answer to this SO similar question with an example. Let me know if you still are not clear afterward.

Upvotes: 1

Related Questions