展示另一个来自SkScene的View Controller [英] Present another View Controller from SkScene
问题描述
我正在尝试从我的"SkScene"中显示另一个viewController
.
这是我的主要viewController
(tuViewController)
I'm trying to present another viewController
from my "SkScene".
This is my main viewController
(tuViewController)
代码:
-(void) openTweetSheet{
FacebookLikeViewDemoViewController *ctrl = [[FacebookLikeViewDemoViewController alloc] initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil];
[self presentViewController:ctrl animated:YES completion:nil];
}
这是我的"SkScene":
This is my "SkScene":
tuViewController *viewController = [[tuViewController alloc]init];
[viewController openTweetSheet];
我要显示的viewController
是FacebookLikeViewDemoViewController
,我需要回到"SkScene".
And the viewController
which i want to present is FacebookLikeViewDemoViewController
and i need to have way back to "SkScene".
然后我得到了sigabrt error
,我尝试了几种显示viewController
的方法,但是总是失败,有一次我换成了viewController
,但是它全是黑色的.我读了很多如何执行该操作,但我个人不知道.感谢您的帮助.
And i got sigabrt error
, i tried few ways to present viewController
but always with failure, one time i got swap to viewController
but it was entirely black. I read a lot how to perform that, but i personally can't figure out. I appreciate your help.
我在通知中心也尝试过.
I tried too with Notification Center.
主viewController
Main viewController
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(goToGameOverViewController:)
name:@"GoToGameOverViewController"
object:nil];
-(void)goToGameOverViewController:(NSNotification *) notification {
FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:@"HelpViewController" bundle:nil];
UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:helpVC animated:YES completion:nil];
}
SkScene
[[NSNotificationCenter defaultCenter]
postNotificationName:@"GoToGameOverViewController" object:self];
但是我遇到了同样的错误.我更想弄清楚为什么通知方式不起作用.
But i got the same error. I prefer to figure out why the way with notification won't work.
推荐答案
根据您的问题,我认为您要进行一些社交媒体发布.
I assume by your question that you are looking to do some social media posting.
您可以将View Controller的引用传递给SKScene,也可以使用NSNotificationCenter
.我更喜欢使用后者.
You can either pass a reference for your View Controller to your SKScene or you can use NSNotificationCenter
instead. I prefer to use the latter.
首先请确保已将Social.framework添加到您的项目中.
First make sure you have added the Social.framework to your project.
将社交框架导入您的View Controller #import <Social/Social.h>
Import the social framework into your View Controller #import <Social/Social.h>
然后在View Controller的viewDidLoad
方法中添加以下代码:
Then in your View Controller's viewDidLoad
method add this code:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(createTweet:)
name:@"CreateTweet"
object:nil];
下一步,将此方法添加到您的View Controller中:
Next add this method to your View Controller:
-(void)createTweet:(NSNotification *)notification
{
NSDictionary *tweetData = [notification userInfo];
NSString *tweetText = (NSString *)[tweetData objectForKey:@"tweetText"];
NSLog(@"%@",tweetText);
// build your tweet, facebook, etc...
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
在您的SKScene中的适当位置(赢得比赛,输掉比赛等等)添加以下代码:
At the appropriate location in your SKScene, (won game, lost game, etc...) add this code:
NSString *tweetText = @"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:tweetText forKey:@"tweetText"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CreateTweet" object:self userInfo:userInfo];
上面的代码发送一个带有文本的NSNotification,您的View Controller将使用该文本并执行指定的方法(在上面的示例中为createTweet).
The above code sends a NSNotification, with text, which your View Controller will pick up and execute the specified method (which is createTweet in the above example).
这篇关于展示另一个来自SkScene的View Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!