iOS,PopOverPresentationController问题,.xib

时间:2015-11-14 21:18:28

标签: ios objective-c iphone ipad nib

我有一个NavigationController,另一个控制器被推入其堆栈:BNRDetailsViewController。现在,在BNRDetailsViewController内部,我试图在按下工具栏按钮时显示弹出窗口,这将显示UIImagePickerController(仅在iPad设备上)。所以,我试着遵循以下stackoverflow线程:

UIPopoverPresentationController on iOS 8 iPhone

但没有成功。如果我只是使用他们的代码,我会收到一个错误,即pushing navigationController is not supported。如果我尝试像这样推送新创建的UIPopoverPresentationController[self.navigationController pushViewController:self.imagePickerPopover animated:YES];它崩溃,因为UIPopoverPresentationController不是UIViewController类型,所以,我想,我不能只是推它在堆栈上。

在这个特殊情况下你会建议做些什么?

以下是我按下工具栏按钮时触发的代码:

- (IBAction)takePicture:(id)sender {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

// If the device have camera, take a picture, otherwise,
// just pick from photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

imagePicker.delegate = self;

// Place image picker on the screen
//[self presentViewController:imagePicker animated:YES completion:NULL];

// Place image picker on the screen
// Check for iPad device before instantiating the popover controller
if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    // Create a new popover controller that will display the imagePicker
    _imagePickerPopover = [[UIPopoverPresentationController alloc] initWithPresentedViewController:imagePicker presentingViewController:self];

    imagePicker.preferredContentSize = CGSizeMake(280, 200);
    _imagePickerPopover.delegate = self;
    _imagePickerPopover.sourceView = self.view;
    CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y + 20;
    _imagePickerPopover.sourceRect = frame;
   // [self.navigationController pushViewController:self.imagePickerPopover animated:YES];
}
}

1 个答案:

答案 0 :(得分:2)

以下是基于您的代码的代码,它将使弹出窗口显示在iPad上。

if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
    imagePicker.modalPresentationStyle = UIModalPresentationPopover;
    imagePicker.preferredContentSize = CGSizeMake(280, 200);
    CGRect frame = [[sender valueForKey:@"view"] frame];
    frame.origin.y = frame.origin.y + 20;

    UIPopoverPresentationController *popoverController = 
        imagePicker.popoverPresentationController;
    popoverController.sourceView = self.view;
    popoverController.sourceRect = frame;

    [self.navigationController presentViewController:imagePicker 
                               animated:YES completion:nil];
}