1

I'm trying to present another viewController from my "SkScene". This is my main viewController(tuViewController)

Code:

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

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

This is my "SkScene":

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

And the viewController which i want to present is FacebookLikeViewDemoViewController and i need to have way back to "SkScene".

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.

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.

sangony
  • 11,636
  • 4
  • 39
  • 55
  • Please have a look at this: http://stackoverflow.com/questions/22421252/how-do-i-present-uiviewcontroller-from-skscene-with-social-framework/22424158#22424158 – ZeMoon May 20 '14 at 10:12
  • please read the Apple documentation fully before continuing, it can be founded here https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/Reference/Reference.html – Prabhu Natarajan May 20 '14 at 10:24
  • I tried to use http://stackoverflow.com/questions/21578391/presenting-uiviewcontroller-from-skscene Notification center resollution but i crashes too.. –  May 20 '14 at 10:34
  • What exception is being raised? – ZeMoon May 20 '14 at 12:09
  • SIGBART When i call FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]initWithNibName:@"FacebookLikeViewDemoViewController" bundle:nil]; [self presentViewController: helpVC animated: YES completion:nil]; It simply crashes. I tried to FacebookLikeViewDemoViewController *helpVC = [[FacebookLikeViewDemoViewController alloc]init]; and then It changes view but it was entirely black screen. –  May 20 '14 at 13:04
  • http://stackoverflow.com/questions/23761194/calling-presentviewcontroller-in-main-viewcontroller-of-spritekit-game I made new topic, with precisely described problem. –  May 20 '14 at 13:29

1 Answers1

2

I assume by your question that you are looking to do some social media posting.

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.

First make sure you have added the Social.framework to your project.

Import the social framework into your View Controller #import <Social/Social.h>

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

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

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

sangony
  • 11,636
  • 4
  • 39
  • 55
  • Thanks, i figure out way with Notification Center, its the best way, but i have another problem, described here > http://stackoverflow.com/questions/23761194/calling-presentviewcontroller-in-main-viewcontroller-of-spritekit-game –  May 20 '14 at 17:16