谁的视图不在窗口层次结构问题中? [英] Whose view is not in the window hierarchy issue?

查看:108
本文介绍了谁的视图不在窗口层次结构问题中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


警告:尝试在窗口层次结构中显示其视图!

Warning: Attempt to present on whose view is not in the window hierarchy!

是的我在发布此问题之前,已经查看了其他问题和答案。他们没有帮我解决这个问题。这就是我在做什么。我正在调用我的单例类socialHelper来显示一个操作表,其中 postToFacebook 方法是一个选项。单击此操作时,我收到上面的警告,并且不显示 postToFacebook 。我从一个UIViewController调用它,其中 UINavigationController 作为主控制器,而我的SocialHelper类是 NSOject

Yes I have looked at the other questions and answers before posting this question. They have not helped me resolve this. Here's what I am doing. I am calling on my singleton class socialHelper to display an action sheet, where postToFacebook method is an option to select. When clicking on this action, I get the Warning above and the postToFacebook is not displayed. I'm calling this from a UIViewController with the UINavigationController as the main controller, and my SocialHelper class is a NSOject.

- (void)postToFacebook
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

    slComposeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
    [slComposeViewController setInitialText:@"Building stairs? Checkout StairsPro on the app store!"];
    [slComposeViewController addImage:[UIImage imageNamed:@"StairsIcon120x120.png"]];
    [slComposeViewController addURL:[NSURL URLWithString:@"https://itunes.apple.com/us/app/stairs-pro/id477032819?ls=1&mt=8"]];

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

    // I've also tried this, which shows the post window, but after hitting cancel or post, my previous viewController is not the current viewController. So my navigation bar is gone and can't move around in my app. 
    // [[[[UIApplication sharedApplication]delegate]window]setRootViewController:slComposeViewController];

    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Facebook Account" message:@"No Facebook account has been configured. You can configure or create a Facebook account in settings." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
        [alert show];
}
}

所以我问的是什么是最好的获得 slComposerViewController 的方法,在这种情况下,我们也可以使用 UIApplication 选项。谢谢!

So what I'm asking is what is the best way to get the slComposerViewController to display in this case, also how we could use the UIApplication option as well. Thank you!

推荐答案

我会改变传递ViewController的方法,以确保正确的VC呈现它如下:

I would change the method to pass in the ViewController to make sure the correct VC is presenting it like so:

- (void)postToFacebookFromVC:(UIViewController*) presentingVC

在调用presentViewController时使用该方法:

And inside that method when calling presentViewController use:

[presentingVC presentViewController:slComposeViewController animated:YES completion:nil];

我认为使用您的socialHelper单例类会是这样的:

I assume then that using your socialHelper singleton class would be something like:

[[socialHelper sharedResource] postToFacebookFromVC:self]; //inside the VC that you want to display it

关于使用rootViewController的第二个问题试试这个:

For your second question on using the rootViewController try this:

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:slComposeViewController animated:YES completion:nil];

编辑:由于前两个建议对您的项目不起作用,请尝试获取topMost ViewController和从中呈现slComposeViewController而不是self,如下所示:

Since the first two suggestions didn't work for your project, try getting the topMost ViewController and presenting slComposeViewController from it instead of "self" like so:

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}

[topController presentViewController:slComposeViewController animated:YES completion:nil];

这篇关于谁的视图不在窗口层次结构问题中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆