Ios UIActionSheet在解雇时发出警告

时间:2013-04-30 22:16:04

标签: ios warnings uiactionsheet

我正在使用UIActionSHeet从多个共享选项中选择(facebook,twitter,mail)

但是在我选择了选项之后我得到了这个警告

 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

但我得到了同样的警告。

1 个答案:

答案 0 :(得分: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: