谁的视图不在窗口层次结构问题中?

Jas*_*son 5 hierarchy uiviewcontroller ios

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

是的,在发布此问题之前,我已查看过其他问题和答案.他们没有帮我解决这个问题.这就是我在做什么.我正在调用我的单例类socialHelper来显示一个操作表,其中postToFacebookmethod是一个选项.单击此操作时,我收到上面的警告,但postToFacebook不显示.我从一个UIViewController调用它UINavigationController作为主控制器,我的SocialHelper类是一个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];
}
}
Run Code Online (Sandbox Code Playgroud)

所以我要问的是slComposerViewController在这种情况下获得显示的最佳方式是什么,以及我们如何使用该UIApplication选项.谢谢!

rym*_*gno 12

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

- (void)postToFacebookFromVC:(UIViewController*) presentingVC
Run Code Online (Sandbox Code Playgroud)

在调用presentViewController使用时,在该方法内部:

[presentingVC presentViewController:slComposeViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

我假设使用您的socialHelper单例类将是这样的:

[[socialHelper sharedResource] postToFacebookFromVC:self]; //inside the VC that you want to display it
Run Code Online (Sandbox Code Playgroud)

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

[[[[[UIApplication sharedApplication] delegate] window] rootViewController] presentViewController:slComposeViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)

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

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

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

[topController presentViewController:slComposeViewController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)