如何从NSObject类提供SLCompose视图控制器 [英] How to present a SLCompose view controller from NSObject Class
问题描述
这是我的代码:
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];
}];
}
但它显示警告
其视图不在窗口层次结构中!
on whose view is not in the window hierarchy!
推荐答案
警告很漂亮明确。您试图通过不在窗口层次结构中的视图来呈现模态视图,这将不起作用。
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.
尝试更改此 [ appDelegate.navController presentViewController:viewController动画:YES完成:nil];
改为:
[appDelegate.window.rootViewController presentViewController:viewController animated:YES completed:nil];
这保证你的应用程序当前活动的根视图控制器将是呈现模态视图控制器的根视图控制器。
This guarantees that the currently active root view controller of your app will be the one presenting the modal view controller.
根据AppDelegate的构建方式,您可能需要添加一个属性或只是一个getter来将窗口
变量显示给外界。
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.
这篇关于如何从NSObject类提供SLCompose视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!