iOS popovers as menu tutorial

2019-08-01 17:37发布

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!

3条回答
来,给爷笑一个
2楼-- · 2019-08-01 17:38

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.

查看更多
戒情不戒烟
3楼-- · 2019-08-01 17:40

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.

查看更多
手持菜刀,她持情操
4楼-- · 2019-08-01 18:05

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

查看更多
登录 后发表回答