在演示过程中尝试在 UITabBarController 上呈现 UIImagePickerController

Posted

技术标签:

【中文标题】在演示过程中尝试在 UITabBarController 上呈现 UIImagePickerController【英文标题】:Attempt to present UIImagePickerController on UITabBarController while a presentation is in progress 【发布时间】:2014-05-27 14:35:32 【问题描述】:

我需要帮助来解决警告错误。我使用UIImagePickerController,当我点击tabBar 上的第三个选项卡时它会显示。当我取消时,我想在选择相机之前显示最后一个选项卡,但是当我关闭我的选择器时,我会收到警告!

这是我的代码,我不明白为什么会收到警告...

-(void)viewWillAppear:(BOOL)animated
    [super viewWillAppear:animated];
    [self showPickerController];



- (void) showPickerController

    imagePicker = [[UIImagePickerController alloc] init];
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
     else
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Erreur !"
                                                          message:@"Votre iPhone n'a pas de Camera"
                                                         delegate:self
                                                cancelButtonTitle:@"Ok"
                                                otherButtonTitles:nil];
        [message show];
    
    [self presentViewController:imagePicker animated:NO completion:nil];


#pragma mark - Camera Methods

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker 
    [self.tabBarController dismissViewControllerAnimated:NO completion:^
        if(self.tabBarController!=nil)
            [self.tabBarController setSelectedIndex:0];
    ];

有什么想法吗?

【问题讨论】:

这将使两个 vcs 同时呈现。将 showPickerController 移动到 viewDidAppear: 【参考方案1】:

除了 danh 的回答之外,在您的视图控制器中添加一个 BOOL 属性,并在您的 viewDidAppear: 方法中添加一些代码:

@property (nonatomic) BOOL appeared;

- (void)viewDidAppear:(BOOL)animated

    [super viewDidAppear:animated];

    if (!self.appeared || [self isMovingToParentViewController] || [self isBeingPresented]) 
        self.appeared = YES;
        [self showPickerController];
    

这将防止UIImagePickerController 在被关闭后立即再次出现。

此外,这与问题并不真正相关,但您可能需要稍微更改您的 showPickerController 方法,以防止在没有可用摄像头时显示空的 UIImagePickerController

- (void)showPickerController

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
        imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.delegate = self;
        [self presentViewController:imagePicker animated:NO completion:nil];
    
    else 
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"Erreur !"
                                                          message:@"Votre iPhone n'a pas de Camera"
                                                         delegate:self
                                                cancelButtonTitle:@"Ok"
                                                otherButtonTitles:nil];
        [message show];
    

【讨论】:

【参考方案2】:

简单的想法:将其更改为:

-(void)viewDidAppear:(BOOL)animated
    [super viewDidAppear:animated];
    [self showPickerController];

可能更好的办法是在开始转换到此 vc 之前检测源类型可用条件,并改为呈现选择器 vc。

【讨论】:

以上是关于在演示过程中尝试在 UITabBarController 上呈现 UIImagePickerController的主要内容,如果未能解决你的问题,请参考以下文章

在演示过程中显示 UIAlertController

尝试在使用OAuth演示者时显示UINavigationController

演示一下在MySQL 5.7下搭建多主一从的过程

python:图形界面演示八皇后搜索过程

python:图形界面演示八皇后搜索过程

在 R 演示文稿中添加本地图像文件