iOS UIPopOverController的使用问题

iOS UIPopOverController的使用问题

今天不经意间发现了UIPopOverController的使用,使用iphone模拟器会出现Crash

crash的原因如下:

'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] called when not running under UIUserInterfaceIdiomPad.'


所以必须使用ipad进行浏览,

所以在代码中使用UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad 

点击打开链接

所以在viewDidLoad的代码如下:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 416)];
    container.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:container];
     
    UIButton *tmpButton = [[UIButton alloc] initWithFrame:CGRectMake(10, 50, 100, 50)];
    tmpButton.backgroundColor = [UIColor brownColor];
    [tmpButton setTitle:@"点击显示" forState:UIButtonTypeCustom];
    [tmpButton addTarget:self action:@selector(btnTest:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tmpButton];
    [tmpButton release];
    
    popTableView = [[PopTableViewController alloc] init];
    
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        popOverController = [[UIPopoverController alloc] initWithContentViewController:popTableView];
        popOverController.delegate = self;
        popOverController.popoverContentSize = CGSizeMake(100, 300);
    }
}

在btnTest方法中实现popOverController显示

点击打开链接

代码如下:

-(void)btnTest:(UIButton *)sender
{
    CGRect presentFromRect = CGRectMake(sender.frame.origin.x, sender.frame.origin.y, sender.frame.size.width, sender.frame.size.height);
    [popOverController presentPopoverFromRect:presentFromRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}

现在可以使用ipad运行下显示了。在iphone模拟器中不会出现crash,但是不会弹出popOverController,显示不出需要的效果

更多阅读请访问http://www.hopean.com


原文地址:https://www.cnblogs.com/hopeanCom/p/2845446.html