How to present a SLCompose view controller from NSObject Class

给你一囗甜甜゛ 提交于 2019-12-08 08:49:08

问题


Here is my code :

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];
     }];
}

But it Shows the warning

on whose view is not in the window hierarchy!


回答1:


The warning is pretty clear. You are trying to present a modal view by a view that is not in the window hierarchy, which won't work.

Try changing this [appDelegate.navController presentViewController:viewController animated:YES completion:nil];

to something like this instead:

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

This guarantees that the currently active root view controller of your app will be the one presenting the modal view controller.

Depending on how your AppDelegate is built you may need to add a property or just a getter to surface the window variable to the outside world.



来源:https://stackoverflow.com/questions/21924528/how-to-present-a-slcompose-view-controller-from-nsobject-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!