iOS 8中SLComposeViewController出现"Attempt to present"错误

6

更新一些针对iOS 8的代码时,我在一个仅适用于iPad的应用程序中遇到了这个错误。在iOS 7中无缺陷的UIActionSheet用于显示分享选项,但使用Facebook和Twitter时却出现问题。

SLComposeViewController *facebookShare = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
//I add an image if there is one        
//I set some initial text    
[self presentViewController:facebookShare animated:YES completion:nil];

很简单的东西。但是在iOS 8上,我得到了下面的警告:
“警告:尝试在PostDetailViewController: 0x180c5200>上呈现SLComposeViewController: 0x1a238760>,而它已经在(null)上呈现。”
我看到了下面的错误,知道它是什么意思。但是它说我的PostDetailViewController正在呈现(null)?!?所以基本上它正在忙于什么事情?
我试图从其他VC的hearty中呈现SLComposeViewController,但我一直收到“尝试呈现错误”的消息。我尝试了从
UIViewController *vc = [UIApplication sharedApplication].keyWindow.rootViewController和self.navigationController进行呈现,但都没有成功。我还尝试从self.navigationController推送SLComposeViewController,这实际上可以工作,但会产生不希望的结果,因为它会推送一个空白背景,然后停留在那里,直到关闭SLComposeViewController并且用户按下返回。
任何线索都将是很好的!提前感谢您。
编辑1:
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

        if (buttonIndex == 0)[self shareWithTwitter];
        if (buttonIndex == 1)[self shareWithFacebook];
        if (buttonIndex == 2)[self shareWithiMessage];
        if (buttonIndex == 3)[self shareWithEmail];
        if (buttonIndex == 4)[self SaveImageToCameraRoll];

}


-(void)shareWithFacebook{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {
        //add animal URL
        SLComposeViewController *facebookShare = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
        [facebookShare addURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.somelink.com/detail.aspx?id=%@",theAnimal.AnimalID]]];

        //add image if there is one
        if (theAnimal.PhotoUrl) {
            [facebookShare addImage:imageView1.image];
        }

        //set initial text
        if ([theAnimal.Sex isEqualToString:@"Male"]) {
            [facebookShare setInitialText:[NSString stringWithFormat:NSLocalizedString(@"pop_his_fb_share", nil),theAnimal.Name]];
        }else [facebookShare setInitialText:[NSString stringWithFormat:NSLocalizedString(@"pop_her_fb_share", nil),theAnimal.Name]];

        [self presentViewController:facebookShare animated:YES completion:nil];


    } else {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"alert", nil)] message:[NSString stringWithFormat:NSLocalizedString(@"details_no_fb_account", nil)] delegate:nil cancelButtonTitle:[NSString stringWithFormat:NSLocalizedString(@"dismiss", nil)] otherButtonTitles:nil, nil];

        [alert show];
    }   
}

你需要展示包含你所发布代码的整个方法,这样我们才能知道你在哪里使用它。 - rdelmar
所以从 actionSheet 代理方法中,我正在调用 [self shareWithFacebook]; - kev
我认为你的代码没有问题。我尝试了一个简化版,它在我的电脑上运行正常。PostDetailViewController 会不会呈现其他东西?你有设置任何segue吗? - rdelmar
PostDetailViewController没有呈现任何内容。这就是为什么错误消息让我感到如此奇怪(呈现(null))。该应用程序还具有一个弹出VC,它也无声地未能呈现Socal,并显示相同的错误消息。 - kev
2个回答

24

我发现在iOS 8中,ActionSheet实际上被视为一个ViewController(至少在此之前我不知道),因此出现了错误信息。当我使用委托方法时,ActionSheet试图在单击按钮时被取消:

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

这个代理在点击时可以获取按钮索引,但是操作表仍然可见。当发生点击事件时,呈现视图控制器会尝试关闭操作表并出现错误。

我切换到:

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

(注:didDismiss)因此,我的共享方法在呈现的视图控制器取消操作表之前不会被调用,然后它可以自由地呈现分享视图控制器!

希望这能帮助其他人。干杯!


1
刚刚遇到了这个问题 - 经过大量搜索后找到了你的解决方案。非常好的答案 - 你应该将其标记为已接受 - 谢谢! - markt

-1

试着做以下的事情。

[[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self presentViewController:fbViewController animated:YES completion:nil];
    }];

我得试一试。实际上,我找到了另一个解决方案,如下所示。 - kev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接