我通过 Sprite Kit 进入 iOS,我认为这是不明智的。
我的目标是在游戏结束时显示“分享”按钮。点击分享按钮应该会显示一个 SLComposeViewController (Twitter Share)。场景的内容不应该改变。
指示“游戏结束”的游戏逻辑存在于 SpriteMyScene.m 中,它是 SKScene 的子类。
我可以通过这种方式在 Game Over 上显示分享按钮:
-(void)update:(CFTimeInterval)currentTime {
if (gameOver){
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:@selector(sendToController)
forControlEvents:UIControlEventTouchDown];
[button setTitle:@"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
}
}
- (void)sendToController
{
NSLog(@"ok");
SpriteViewController *viewController = [SpriteViewController alloc];
[viewController openTweetSheet];
}
我被卡住的地方是试图让 showTweetButton 方法工作。我的 SpriteViewController.m 看起来像这样:
- (void)openTweetSheet
{
SLComposeViewController *tweetSheet = [SLComposeViewController
composeViewControllerForServiceType:
SLServiceTypeTwitter];
// Sets the completion handler. Note that we don't know which thread the
// block will be called on, so we need to ensure that any required UI
// updates occur on the main queue
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
switch(result) {
// This means the user cancelled without sending the Tweet
case SLComposeViewControllerResultCancelled:
break;
// This means the user hit 'Send'
case SLComposeViewControllerResultDone:
break;
}
};
// Set the initial body of the Tweet
[tweetSheet setInitialText:@"just setting up my twttr"];
// Adds an image to the Tweet. For demo purposes, assume we have an
// image named 'larry.png' that we wish to attach
if (![tweetSheet addImage:[UIImage imageNamed:@"larry.png"]]) {
NSLog(@"Unable to add the image!");
}
// Add an URL to the Tweet. You can add multiple URLs.
if (![tweetSheet addURL:[NSURL URLWithString:@"http://twitter.com/"]]){
NSLog(@"Unable to add the URL!");
}
// Presents the Tweet Sheet to the user
[self presentViewController:tweetSheet animated:NO completion:^{
NSLog(@"Tweet sheet has been presented.");
}];
}
我总是在日志中得到类似的东西:
-[UIView presentScene:]: unrecognized selector sent to instance 0x13e63d00 2013-10-17 18:40:01.611 Fix[33409:a0b] *由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[UIView presentScene:]:无法识别的选择器发送到实例 0x13e63d00'