将分数发布到facebook
我在将分数发布到facebook,facebook,sprite-kit,slcomposeviewcontroller,Facebook,Sprite Kit,Slcomposeviewcontroller,我在SpriteKit中用xcode创建了一个应用程序,游戏结束后,它会显示你的分数,我想添加这个功能,将你的分数发布到facebook上。我几乎所有的代码都在MyScene.m中,无法访问presentViewController。只有我的ViewController.m文件可以访问它,所以我尝试从Myscene.m调用ViewController中的一个实例方法,但我找不到一种方法。我发现从其他文件调用方法的唯一方法是使用+(void),我认为这是一种类方法 Myscene.m: i
SpriteKit
中用xcode创建了一个应用程序,游戏结束后,它会显示你的分数,我想添加这个功能,将你的分数发布到facebook上。我几乎所有的代码都在MyScene.m
中,无法访问presentViewController
。只有我的ViewController.m文件可以访问它,所以我尝试从Myscene.m调用ViewController中的一个实例方法,但我找不到一种方法。我发现从其他文件调用方法的唯一方法是使用+(void)
,我认为这是一种类方法
Myscene.m:
if (location.x < 315 && location.x > 261 && location.y < 404 && location.y > 361) {
//if you click the post to facebook button (btw, location is a variable for where you tapped on the screen)
[ViewController facebook];
}
这样就成功了,它正确地调用了facebook类方法,但是当我在setInitialText括号后加上[self-presentViewController:facebook-animated:YES]
时,它给了我这个错误:选择器“presentViewController:animated:”没有已知的类方法
顺便说一下,它允许我在实例方法中使用
presentViewController
,但我不能从类方法内部或从Myscene文件调用该方法。有没有办法从另一个文件调用实例方法,或者从类方法访问presentViewController
?谢谢您可以将视图控制器的引用传递到场景,也可以使用NSNotificationCenter
。我更喜欢用后者
首先确保已将Social.framework添加到项目中
将社交框架导入视图控制器#导入
然后在视图控制器的viewDidLoad方法中添加以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(createPost:)
name:@"CreatePost"
object:nil];
NSString *postText = @"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:postText forKey:@"postText"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CreatePost" object:self userInfo:userInfo];
接下来,将此方法添加到视图控制器:
-(void)createPost:(NSNotification *)notification
{
NSDictionary *postData = [notification userInfo];
NSString *postText = (NSString *)[postData objectForKey:@"postText"];
NSLog(@"%@",postText);
// build your tweet, facebook, etc...
SLComposeViewController *mySLComposerSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[self presentViewController:mySLComposerSheet animated:YES completion:nil];
}
在场景中的适当位置(赢局、输局等)添加以下代码:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(createPost:)
name:@"CreatePost"
object:nil];
NSString *postText = @"I just beat the last level.";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:postText forKey:@"postText"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"CreatePost" object:self userInfo:userInfo];
上面的代码发送一个带有文本的NSNotification,您的视图控制器将接收并执行指定的方法。@user3386154-太好了。如果它成功了,请检查我的答案是否被接受。谢谢。公认的答案没有涉及这一点,但您提到的错误是由于您试图调用类的实例方法造成的self
类方法内部(+(void)facebook
)指的是[ViewController类]
,而不是ViewController*yourViewController
。并且ViewController
类不能显示视图控制器。