Present another View Controller from SkScene

前端 未结 1 1303
一个人的身影
一个人的身影 2021-01-25 06:39

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

Code:

-(voi         


        
相关标签:
1条回答
  • 2021-01-25 06:40

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

    0 讨论(0)
提交回复
热议问题