从UITabBarController驾驶UIImagePickerController

问题描述:

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

我的问题: 我有MainCameraTabController从UIViewController继承,并作为编排的启动UIImagePickerController和选择器的委托的类。当机械手完成后,我尝试推出另一名来自MainCameraTabController不同的视图控制器,但我得到的错误,

*** 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。不知道为什么会这样。

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

对于意见,我有类似的经验,我所做的是切换视图的顺序。我发起了这样一个观点,即我想在选取器之后首先显示,在viewDidload方法中,我创建并显示选取器,因此在选取器被解散后,它将显示我想要的视图。如果你想让它们看起来很自然,你可以随时使用视图的hidden属性来平滑流动。