如何从NSObject类呈现SLCompose视图控制器

时间:2014-02-21 03:08:52

标签: objective-c facebook slcomposeviewcontroller

这是我的代码:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *viewController  = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter]; //Tell him with what social platform to use it, e.g. facebook or twitter

    [viewController setInitialText:sQuotes];

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate.navController presentViewController:viewController animated:YES completion:nil];


    [viewController setCompletionHandler:^(SLComposeViewControllerResult result)
     {
         NSString *output;
         switch (result)
         {
             case SLComposeViewControllerResultCancelled:
                 output = @"Action Cancelled";
                 break;
             case SLComposeViewControllerResultDone:
             {
                 output = @"Post Successfull";

             }
                 break;
             default:
                 break;
         }

         [appDelegate.navController dismissViewControllerAnimated:YES completion:nil];
     }];
}

但它会显示警告

其视图不在窗口层次结构中!

1 个答案:

答案 0 :(得分:1)

警告很清楚。您试图通过不在窗口层次结构中的视图来呈现模式视图,这将不起作用。

尝试更改此[appDelegate.navController presentViewController:viewController animated:YES completion:nil];

代替这样的事情:

[appDelegate.window.rootViewController presentViewController:viewController animated:YES completion:nil];

这可以保证您应用的当前活动根视图控制器将是呈现模态视图控制器的控制器。

根据AppDelegate的构建方式,您可能需要添加一个属性或只是一个getter来将window变量显示给外界。