更新:iOS8 版本已經不可用
為 UIPopoverController 增加類別,如下:
//NSObject+UIPopover_Iphone.h #import <Foundation/Foundation.h> @interface UIPopoverController (overrides) +(BOOL)_popoversDisabled; @end //NSObject+UIPopover_Iphone.m #import "NSObject+UIPopover_Iphone.h" @implementation UIPopoverController (overrides) +(BOOL)_popoversDisabled { return NO; } @end
在要使用的 ViewController 中,導入頭文件 NSObject+UIPopover_Iphone.h,便可正常使用。
注意
需要注意的是,保持將要顯示的 popoverViewController 的引用,否則在 ARC 中無法使用,具體表現如下
*** Terminating app due to uncaught exception 'NSGenericException', reason: '-[UIPopoverController dealloc] reached while popover is still visible.'
與此相似的有,創建一個 UIWindow,如果沒有保持對 UIWindow 的引用,當 makeKeyAndVisible 后,window 只會一閃而過。
想必這種一定不是想要的效果。
使用
- (IBAction)moreItemClicked:(id)sender { UITableViewController *detailController = [self.storyboard instantiateViewControllerWithIdentifier:@"MorePopover"]; //hanlde tableview delegate in this class detailController.tableView.delegate = self; UIPopoverController *popoverController = nil; popoverController = [[UIPopoverController alloc] initWithContentViewController:detailController]; popoverController.delegate = self; popoverController.popoverContentSize = CGSizeMake(140, 88); [popoverController presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES]; self.poc = popoverController; }
效果

