如何让用户选择UIImagePickerController的源类型?

时间:2012-02-29 19:15:34

标签: objective-c ios uinavigationcontroller uiimagepickercontroller uipopovercontroller

首先让我告诉你我要做的事情:

  1. 当用户点击某个按钮时,我想提供一个UIPopoverController UINavigationController作为其contentViewController(这是在iPad上)
  2. 推入UIViewController的第一个UINavigationController将是一个自定义视图控制器,它有两个按钮:“拍照”和“从库中选择”。
  3. 触摸setSourceType:UIImagePickerControllerSourceTypeCamera上的“拍照”按钮UIImagePickerController并将其推入UINavigationController
  4. 点击setSourceType:UIImagePickerControllerSourceTypePhotoLibrary上的UIImagePickerController点击“从库中选择”按钮并将其推入UINavigationController
  5. 我希望能够完成所有这些并且能够允许用户在UINavigationController允许的情况下向后导航。

    问题是,由于UIImagePickerController也是UINavigationController,我无法按照#3和#4中的规定将其推送到另一个UINavigationController

    问题:

    1. 我有办法在相机/照片库视图控制器之前使用UIImagePickerController和自定义视图控制器吗?
    2. 或者相反,有没有办法让我可以访问相机/照片库视图控制器,将它们推送到另一个UINavigationController
    3. 或者还有另一种我在这里完全失踪的方式吗?

1 个答案:

答案 0 :(得分:0)

添加以下代码(如详细here)最终为我解决了这个问题。

使用navigationController:willShowViewController:animated:方法访问navigationBar。

然后使用此代码,您可以添加“取消”按钮。

if ([navigationController isKindOfClass:[UIImagePickerController class]]) {

    UINavigationBar *bar = navigationController.navigationBar;
    UINavigationItem *top = bar.topItem;

    UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(imagePickerControllerDidCancel:)];
    [top setLeftBarButtonItem:cancel];

} else { 

    //do non imagePickerController things 

}
相关问题