从SkScene展示另一个View Controller

时间:2014-05-20 10:09:37

标签: sprite-kit nsnotifications social-framework

我试图从我的" SkScene"中提出另一个viewController。 这是我的主要viewController(tuViewController)

代码:

-(void) openTweetSheet{
    FacebookLikeViewDemoViewController *ctrl = [[FacebookLikeViewDemoViewController alloc] initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil];

    [self presentViewController:ctrl animated:YES completion:nil];
}

这是我的" SkScene":

tuViewController *viewController = [[tuViewController alloc]init];
[viewController openTweetSheet];

我要呈现的viewControllerFacebookLikeViewDemoViewController,我需要回到" SkScene"。

我得到了sigabrt error,我尝试了几种方式来呈现viewController但总是失败,有一次我换到了viewController,但它完全是黑色的。我读了很多如何表演,但我个人无法弄明白。感谢您的帮助。

我也尝试过通知中心。

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

但是我得到了同样的错误。我更愿意弄清楚为什么通知的方式不起作用。

1 个答案:

答案 0 :(得分:2)

我假设你想要做一些社交媒体发布。

您可以将View Controller的引用传递给SKScene,也可以使用NSNotificationCenter。我更喜欢使用后者。

首先确保已将Social.framework添加到项目中。

将社交框架导入View Controller #import <Social/Social.h>

然后在View Controller的viewDidLoad方法中添加以下代码:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(createTweet:)
                                             name:@"CreateTweet"
                                           object:nil];

接下来将此方法添加到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的适当位置,(赢得比赛,输掉游戏等...)添加以下代码:

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)。

相关问题