从 SkScene 展示另一个视图控制器 [英] 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 错误
,我尝试了几种方法来呈现 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.
我也尝试过通知中心.
主视图控制器
[[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.
您可以将视图控制器的引用传递给您的 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.
将社交框架导入您的视图控制器#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];
接下来将此方法添加到您的视图控制器:
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 展示另一个视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!