0

我正在使用 UIActionSHeet 从多个共享选项(facebook、twitter、邮件)中进行选择

但是在我选择该选项后,我收到了这个警告

 Warning: Attempt to dismiss from view controller <UINavigationController: 0x1ed7eaf0> while a presentation or dismiss is in progress 

这是我使用的代码:

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    //BOOL displayedNativeDialog;
    SLComposeViewController * tw =  [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    //TWTweetComposeViewController * tw = [[TWTweetComposeViewController alloc]init];
    tw.completionHandler = ^(TWTweetComposeViewControllerResult result)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    };

    SLComposeViewController * fb = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    fb.completionHandler = ^(SLComposeViewControllerResult result)
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    };

    MFMailComposeViewController * mail = [[MFMailComposeViewController alloc]init];
    [mail setMailComposeDelegate:self];
    [mail.navigationBar setBackgroundColor:[UIColor blackColor]];
    NSData * imageData = [NSData dataWithData:UIImagePNGRepresentation(previewImageView.image)];
    switch (buttonIndex) {
        case 0:
            UIImageWriteToSavedPhotosAlbum(previewImageView.image, nil, nil, nil);
            break;
        case 1:
            [fb addImage:previewImageView.image];
            [self presentViewController:fb animated:YES completion:nil];
            fb = nil;       
            break;
        case 2:
            [actionSheet dismissWithClickedButtonIndex:-1 animated:YES];
            [tw addImage:previewImageView.image];
            [self presentViewController:tw animated:YES completion:nil];
            break;
        case 3:
            [mail addAttachmentData:imageData mimeType:@"image/png" fileName:@"Image.png"];
            [self presentViewController:mail animated:YES completion:nil];
            break;

        default:
            break;
    }
}

我也尝试使用委托功能:

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex

但我得到了同样的警告。

4

1 回答 1

2

1)你应该有 ivars 保持对这些的强烈引用:

SLComposeViewController * tw    
SLComposeViewController * fb    
MFMailComposeViewController * mail

创建这些是不好的形式,然后让 ARC 回收它们 - 为什么不根据所选选项创建您需要的那些?您可以在 case 语句中创建对象:

case 1:
{
    NSObject *obj = ....
    ....
}   break;

2)您不需要关闭 actionSheet - 单击按钮即可完成。

3)不要在actionSheet委托调用中显示视图控制器,向主队列发布一个块来做到这一点:

dispatch_async(dispatch_get_main_queue(), ^ {
    [self presentViewController:tw animated:YES completion:nil];
} );

这有点像把那条线放在一个方法中,然后做一个performSelectorOnMainThread:

于 2013-04-30T22:32:16.660 回答