iOS,PopOverPresentationController 问题,.xib

iOS, PopOverPresentationController issue, .xib

我有一个 NavigationController,另一个控制器被压入其堆栈:BNRDetailsViewController。现在,在 BNRDetailsViewController 中,我试图在按下工具栏按钮时显示弹出窗口 window,这会向我显示 UIImagePickerController(仅在 iPad 设备上)。因此,我尝试关注以下 Whosebug 线程:

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];
}
}

这是基于您的代码的代码,它将使弹出框显示在 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];
}