iPhone: 在 iPhone app 里使用 UIPopoverController

 更新: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;
}

效果

原文地址:https://www.cnblogs.com/ihojin/p/iphone-uipopovercontroller.html