iOS,PopOverPresentationController问题,.xib

iOS,PopOverPresentationController问题,.xib,ios,objective-c,iphone,ipad,nib,Ios,Objective C,Iphone,Ipad,Nib,我有一个导航控制器,另一个控制器被推到它的堆栈上:BNRDetailsViewController。现在,在BNRDetailsViewController中,我试图在按下工具栏按钮时显示一个弹出窗口,这将向我展示UIImagePickerController(仅在iPad设备上)。因此,我尝试遵循以下stackoverflow线程: 但是没有成功。如果我只是使用他们的代码,我会得到一个错误,即不支持推送navigationController。如果我尝试按如下方式推送新创建的UIPopove

我有一个导航控制器,另一个控制器被推到它的堆栈上:BNRDetailsViewController。现在,在BNRDetailsViewController中,我试图在按下工具栏按钮时显示一个弹出窗口,这将向我展示UIImagePickerController(仅在iPad设备上)。因此,我尝试遵循以下stackoverflow线程:

但是没有成功。如果我只是使用他们的代码,我会得到一个错误,即不支持
推送navigationController
。如果我尝试按如下方式推送新创建的
UIPopoverPresentationController
[self.navigationController推送视图控制器:self.ImagePickerPover动画:是]它崩溃是因为
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];
}
}

以下是基于您的代码的代码,它将使popover显示在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];
}

基本上,您希望在iOS 8+上演示一个popover。请参阅此问题中的示例代码:。该代码在iPhone和iPad上都显示了popover。如果您希望popover仅显示在iPad上,而不显示在iPhone上,请返回相应的
UIModalPresentationStyle
值。谢谢!。但是,我不使用故事板,而是使用.xib文件。所以,我仍然不知道在这种情况下该怎么办。好的,我知道了。只需更改一行:
popoverController.sourceView=self.view
popoverController.sourceView=self.toolbar
,其中
toolbar
被定义为BNRDetailsViewController类中的属性:
@属性(弱,非原子)IBOutlet UIToolbar*工具栏
只需注意:PreferredContSize取代了popoverContentSize