我通过 Sprite Kit 进入 iOS,我认为这是不明智的。
我的目标是在游戏结束时显示“分享”按钮。点击共享按钮应该会显示一个 SLComposeViewController(Twitter 共享)。场景的内容不应该改变。
指示“游戏结束”的游戏逻辑存在于 SKScene 的子类 SpriteMyScene.m 中。
我可以通过这种方式在游戏结束时显示分享按钮:
-(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] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView presentScene:]: unrecognized selector sent to instance 0x13e63d00'
最佳答案
您正在创建一个新的 View Controller 但从未呈现它:
SpriteViewController *viewController = [SpriteViewController alloc];
我假设 SpriteViewController
是呈现您的 SpriteMyScene
的,您希望将控制权交还给呈现 SpriteViewController
。
您需要在 SpriteMyScene
子类中保留对 SpriteViewController
的引用,然后在调用 openTweetSheet
时访问该引用。
在 SpriteMyScene.h 中
@class SpriteViewController;
@interface SpriteMyScene : SKScene
@property (nonatomic, weak) SpriteViewController *spriteViewController;
@end
在 SpriteViewController.m 中
// somewhere you initialize your SpriteMyScene object, I'm going to call it myScene
myScene.spriteViewController = self;
在 SpriteMyScene.m 中
#import "SpriteViewController.h"
- (void)sendToController
{
NSLog(@"ok");
// use the already-created spriteViewController
[_spriteViewController openTweetSheet];
}
关于ios - 如何从 SKScene 呈现 UIViewController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438719/