objective-c - 如何允许用户选择 UIImagePickerController 的源类型?

标签 objective-c ios uinavigationcontroller uiimagepickercontroller uipopovercontroller

首先让我告诉你我正在尝试做什么:

  1. 当用户点击一个按钮时,我想呈现一个 UIPopoverController 和一个 UINavigationController 作为它的 contentViewController(这是在 iPad 上)
  2. 第一个插入 UINavigationControllerUIViewController 将是一个自定义 View Controller ,它有两个按钮:“拍照”和“从库中选择”。
  3. 触摸“拍照”按钮将 UIImagePickerController 上的 setSourceType:UIImagePickerControllerSourceTypeCamera 并将其推送到 UINavigationController
  4. 触摸“从库中选择”按钮将 UIImagePickerController 上的 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary 并将其推送到 UINavigationController

我希望能够完成所有这些,并能够允许用户在 UINavigationController 允许的情况下向后导航。

问题是,由于 UIImagePickerController 也是一个 UINavigationController,我无法将它推送到另一个 UINavigationController 中,如#3 和#4 中所述.

问题:

  1. 有没有办法让我使用 UIImagePickerController 并将自定义 View Controller 推送到相机/照片库 View Controller 之前?
  2. 或者相反,有没有办法让我访问相机/照片库 View Controller 以将它们推送到不同的 UINavigationController
  3. 或者还有另一种我完全想念的方式吗?

最佳答案

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

使用 navigationController:willShowViewController:animated: 方法访问导航栏。

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

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 

}

关于objective-c - 如何允许用户选择 UIImagePickerController 的源类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505207/

相关文章:

ios - UniChar(来自 CFString)和 unichar(来自 NSString)是一样的吗?

ios - 试图创建一个彩色圆圈 UIImage,但它总是以方形结束。为什么?

ios - 使用 CIFilter 对 Photoshop LUT 滤镜进行颜色校正

ios - 如何在 Xcode + Swift 4 中制作自定义 UIBarButtonItem 类?

swift - 推 View Controller : unexpectedly found nil while unwrapping an Optional value in SWIFT

iphone - UITabBarController 中的 UINavigationController,设置导航 Controller 标题

iphone - UIButton 在第一次点击时不工作

objective-c - Objective-C 类别和扩展的细节

ios - 通过_property样式捕获属性值导致retain循环

objective-c - 何时使用 InterfaceBuilder 构建 View ?