尝试显示...其视图不在窗口层次结构中

时间:2014-04-17 11:12:11

标签: ios objective-c view hierarchy

我使用WinDev Mobile开发,它生成一个项目,然后我用XCode打开进行编译。 只有在Facebook上的WM共享功能尚未开发,我必须在ObjC中开发

我从代码开始:

if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

    SLComposeViewController *controllerSLC = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook] ;
    [controllerSLC setInitialText:myMessage];
    [controllerSLC addURL:[NSURL URLWithString:myUrl]];
    [controllerSLC addImage:[UIImage imageNamed:myImage]];

    // NSLog (@"% @", [UIApplication sharedApplication] keyWindow.rootViewController.)

    [self presentViewController:controllerSLC animated:YES completion:Nil];

    controllerSLC.completionHandler = ^(SLComposeViewControllerResult result) {
        NSString *output = nil;
        switch(result) {
            SLComposeViewControllerResultDone box:
                output = @ "Your tweet has-been sent" ;
                status = 2;
                // NSLog (@" SENT ");
                break;
            SLComposeViewControllerResultCancelled box:
                output = @"Your tweet has-been canceled ";
                status = 3;
                // NSLog (@" canceled ");
                break;
            default:
                break;
        }
        [controllerSLC dismissViewControllerAnimated:YES completion:nil] ;
    };

}

我只有一个错误:

Warning Attempt to present <SLComposeViewController > on <CFenPrincipaleViewController> Whose view is not in the window hierarchy !

我添加了代码:

UIViewController *activeController = [UIApplication sharedApplication] keyWindow.rootViewController. ;
if ([ activeController isKindOfClass:[UINavigationController class]]) {
    activeController = [(UINavigationController *)activeController visibleViewController] ;
}
[activeController presentViewController:controllerSLC animated:YES completion:Nil];

它适用于窗口主页应用程序,我没有错误。

但是当我在另一个窗口调用相同的函数时,错误返回.... 提前感谢您的帮助

1 个答案:

答案 0 :(得分:4)

我有同样的问题......试一试!

首先获取根视图控制器....

-(UIViewController*) getRootViewController {
return [UIApplication sharedApplication].keyWindow.rootViewController;
}

现在而不是activeController = [(UINavigationController *)activeController visibleViewController] ;

这样做:

UIViewController* activeController = [self getRootViewController];

现在你的最后一行[activeController presentViewController:controllerSLC animated:YES completion:Nil];应该有效。

希望这有帮助。