您的位置:首页 > 产品设计 > UI/UE

UIPopOverController的使用问题

2016-05-26 14:39 447 查看


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

crash的原因如下:

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

 

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

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

 

所以在viewDidLoad的代码如下:

[cpp] view plaincopy

- (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显示

 

代码如下:

[cpp] view plaincopy

-(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,显示不出需要的效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: