从UITabBarController驱动UIImagePickerController

时间:2011-03-17 23:33:58

标签: iphone uiimagepickercontroller

这是我的目标: 我想从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 个答案:

答案 0 :(得分:0)

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

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