您好,匿名用户
随意问技术百科期待您的加入

在UIViewController中使用UIImagePickerController出现NSInvalidArgumentException

0 投票

在某个UIViewController中出现了这个错误,以下是代码

@interface TestViewController : UIViewController <ICALViewDelegate, UIWebViewDelegate,
    UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
    UIButton *_avatar;
    UIImagePickerController *_avatarPicker;
}
@end

- (id)init
{
...
        _avatar = [[UIButton alloc] initWithFrame:avatarRect];
        [_avatar setBackgroundColor:[UIColor blackColor]];
        [_avatar addTarget:self action:@selector(pickAvatar:) forControlEvents:UIControlEventAllTouchEvents];
        [[self view] addSubview:_avatar];
        
        _avatarPicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [_avatarPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [_avatarPicker setMediaTypes:[UIImagePickerController availableMediaTypesForSourceType:[_avatarPicker sourceType]]];
        }
        [_avatarPicker setDelegate:self];
        [_avatarPicker setAllowsEditing:YES];
...
}

- (void)pickAvatar:(id)sender
{
    [self presentModalViewController:_avatarPicker animated:YES];
}

当点击_avatar这个按钮时会出现这个错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally an active controller <TestViewController: 0x689b2d0>.'
*** First throw call stack:
(0x149c052 0x172ed0a 0x5ae98d 0x5af58c 0x5af5cc 0xfdbc 0x149dec9 0x4e55c2 0x4e555a 0x58ab76 0x58b03f 0x589290 0x58a49f 0x50aac0 0x50ac56 0x4f1384 0x4ef6cf 0x4ef004 0x4ef682 0x4e4a0f 0x227cfa9 0x14701c5 0x13d5022 0x13d390a 0x13d2db4 0x13d2ccb 0x227b879 0x227b93e 0x4e2a9b 0x1fa8 0x1f05 0x1)
terminate called throwing an exception

似乎意思是不能从一个模态窗口再弹出另一个模态窗口,不知道如何解决这个问题。

用户头像 提问 2012年 12月1日 @ Ziggs 上等兵 (345 威望)
分享到:

1个回答

0 投票

怀疑是

[self presentModalViewController:_avatarPicker animated:YES];

这个时候 _avatarPicker 没了
建议这样做

- (id)init
{
...
    _avatar = [[UIButton alloc] initWithFrame:avatarRect];
    [_avatar setBackgroundColor:[UIColor blackColor]];
    [_avatar addTarget:self action:@selector(pickAvatar:) forControlEvents:UIControlEventAllTouchEvents];
    [[self view] addSubview:_avatar];        
...
}

- (void)pickAvatar:(id)sender
{
    if (nil == _avatarPicker) {
        _avatarPicker = [[UIImagePickerController alloc] init];
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            [_avatarPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
            [_avatarPicker setMediaTypes:[UIImagePickerController availableMediaTypesForSourceType:[_avatarPicker sourceType]]];
        }
        [_avatarPicker setDelegate:self];
        [_avatarPicker setAllowsEditing:YES];
    }
    [self presentModalViewController:_avatarPicker animated:YES];
}
用户头像 回复 2012年 12月1日 @ Galio 上等兵 (289 威望)
提一个问题:

相关问题

0 投票
1 回复 46 阅读
0 投票
0 回复 38 阅读
0 投票
1 回复 57 阅读
+1 投票
1 回复 69 阅读

欢迎来到随意问技术百科, 这是一个面向专业开发者的IT问答网站,提供途径助开发者查找IT技术方案,解决程序bug和网站运维难题等。
温馨提示:本网站禁止用户发布与IT技术无关的、粗浅的、毫无意义的或者违法国家法规的等不合理内容,谢谢支持。

欢迎访问随意问技术百科,为了给您提供更好的服务,请及时反馈您的意见。
...