iOS popovers as menu tutorial

£可爱£侵袭症+ 提交于 2020-01-15 11:44:46

问题


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!


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/9284600/ios-popovers-as-menu-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!