从 UITabBarController 驱动 UIImagePickerController

Posted

技术标签:

【中文标题】从 UITabBarController 驱动 UIImagePickerController【英文标题】:Driving UIImagePickerController from UITabBarController 【发布时间】:2011-03-17 23:33:58 【问题描述】:

这是我的目标: 我想从 TabBarController 显示 UIImagePickerController,一旦拍摄照片,我希望“使用”按钮带我另一个视图控制器。

我的问题: 我有 MainCameraTabController,它继承自 UIViewController 并用作协调 UIImagePickerController 和选择器委托的启动的类。选择器完成后,我尝试从 MainCameraTabController 启动另一个不同的 ViewController 但我收到错误,

*** Assertion failure in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:

如果我在 UIImagePickerController 被解除和我启动下一个控制器之间设置一个定时延迟,它可以正常工作,但我想更优雅地做到这一点。

有没有更好的方法来构建我的类继承,以便我可以让 MainCameraTabController 显示 Picker,然后显示第二个视图控制器?

// #
// # 1. Create the tab bar and add the MainCameraTabController:
// #
// tab1Controller and tab3Controller are also created
cameraTabController = [[MainCameraTabController alloc] init];

tabBarController = [[UITabBarController alloc] init];                                                                                                      

NSArray *tabViewControllers = [NSArray arrayWithObjects:tab1Controller, 
                                            cameraTabController
                                            tab3Controller, nil];

tabBarController.viewControllers = tabViewControllers;

self.window.rootViewController = self.tabBarController;


// #
// # 2. MainCameraTabController interface & implementation
// #

@interface MainCameraTabController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>


- (void)showCamera;

@end

@implementation MainCameraTabController

// #
// # 3. Show the camera when the view loads
// #
- (void)viewDidLoad

    [self startCameraController:self usingDelegate:self];


- (void)showNextController

    FollowupController *fc = [[FollowupController alloc] initWithNibName:@"SomeView" bundle:nil];

    //  THIS IS THE PROBLEM
    [self presentModalViewController:cameraPicker animated: YES];


- (BOOL)startCameraController:(UIViewController *)controller
                usingDelegate:(id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)pickerDelegate 


    UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
    // configure the cameraPicker

    // #
    // # Apple's doc specifies that UIImagePickerController must be launched with   
    // # presentModalViewController
    // #

    [controller presentModalViewController:cameraPicker animated: YES];    


// UIImagePickerControllerDelegate method called when photo taking is finished
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 

    // work to handle the photo

    // Dismiss the picker
    [[picker parentViewController] dismissModalViewControllerAnimated: YES];
    [picker release];    

    [self showNextController];


@end

在相关的注释中,我检查了选择器的 parentViewController 是什么时候

imagePickerController:didFinishPickingMediaWithInfo

被调用,并且父级不是 MainCameraTabController 而是 UITabBarController。不知道为什么会这样。

【问题讨论】:

【参考方案1】:

由于关闭视图时的动画,您需要延迟动画完成才能执行下一个动画。您可以通过将animated 设置为NO 来解决此问题。这将剪切动画并立即执行下一个。

对于视图,我有类似的经历,我所做的就是切换视图的顺序。我首先启动了我想在选择器之后显示的视图,在viewDidload 方法中,我创建并显示了选择器,因此在选择器被关闭后,它将显示我想要的视图。如果您想让它们看起来自然,您可以随时使用视图的hidden 属性来平滑流程。

【讨论】:

以上是关于从 UITabBarController 驱动 UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章

从一个 UITabBarController 导航到另一个 UITabBarController

如果我使用 UITabBarController,是不是需要在所有屏幕中添加导航栏项目?

无法从模态 ViewController 访问 UITabBarController

目前从 Appdelegate 快速转到 UITabBarController

如何从 View 获取数据到 UITabBarController

UISplitview:从 appDelegate 访问 UITabBarController