标签:
今天不经意间发现了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,显示不出需要的效果
标签:
原文地址:http://my.oschina.net/hejunbinlan/blog/469894