警告:尝试在未在窗口层次结构中的视图上呈现。
是的,我在发布这个问题之前已经查看了其他问题和答案。它们没有帮助我解决这个问题。这是我正在做的事情。我正在调用我的单例类socialHelper来显示一个操作表,其中postToFacebook
方法是一个可选项。当点击此操作时,我收到上面的警告,并且postToFacebook
未显示。我从UIViewController中调用此功能,其主控制器为UINavigationController
,而我的SocialHelper类是NSObject
。
- (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];
}
}
所以我的问题是,在这种情况下,最好的方法是让 slComposerViewController
显示出来,另外我们如何使用 UIApplication
选项。谢谢!
presentViewController:animated:completion:
需要从当前屏幕上呈现的 viewController 中调用。如果SocialHelper
不是当前呈现的 viewController,则不应在其上调用此方法。 - Paul.s