我在尝试制作一个允许用户在社交媒体网站上分享我的游戏的分享按钮时遇到了很多困难。我正在使用Sprite Kit开发游戏。从我所看到的,我的印象是启动共享viewController的实际代码必须在我的GameViewController类下。因此,我有一个在gameScene类中初始化的按钮,当它被按下时,运行一个创建gameViewController类实例的方法,然后运行shareGame方法。这是我的代码,我很感激任何帮助,因为我真的迷失了。
在我的gameScene类中(按下按钮时运行此方法)
-(IBAction)runShareGame:(id)sender{
GameViewController *myViewController = [[GameViewController alloc] init];
[myViewController shareGame];
}
此代码位于gameViewController.m类中(请注意,shareGame方法包含在界面中)
-(void)shareGame{
NSLog(@"shareGame!");
NSString *message = @"Hello World!";
UIImage *imageToShare = [UIImage imageNamed:@"blue"];
NSArray *postItems = @[message, imageToShare];
UIActivityViewController *activityVC = [[UIActivityViewController alloc]
initWithActivityItems:postItems
applicationActivities:nil];
[self presentViewController:activityVC animated:YES completion:nil];
}
当我运行代码时,我在控制台中收到以下消息:
shareGame!
2015-06-06 17:01:27.626 Dodge[96002:3651805] -[UIView setShowsFPS:]: unrecognized selector sent to instance 0x7a8c88b0
2015-06-06 17:01:27.630 Dodge[96002:3651805] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView setShowsFPS:]: unrecognized selector sent to instance 0x7a8c88b0'
First throw call stack:
(
0 CoreFoundation 0x00966746 __exceptionPreprocess + 182
1 libobjc.A.dylib 0x005efa97 objc_exception_throw + 44
2 CoreFoundation 0x0096e705 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3 CoreFoundation 0x008b5287 ___forwarding___ + 1047
4 CoreFoundation 0x008b4e4e _CF_forwarding_prep_0 + 14
5 Dodge 0x000fb48b -[GameViewController viewDidLoad] + 139
6 UIKit 0x0127cda4 -[UIViewController loadViewIfRequired] + 771
7 UIKit 0x0127d095 -[UIViewController view] + 35
8 UIKit 0x0125784d -[UIPresentationController __sizeClassPair] + 54
9 UIKit 0x0128a7a3 -[UIViewController _presentViewController:withAnimationController:completion:] + 2349
10 UIKit 0x0128d382 __62-[UIViewController presentViewController:animated:completion:]_block_invoke + 345
11 UIKit 0x0128d1d4 -[UIViewController presentViewController:animated:completion:] + 224
12 Dodge 0x000fb817 -[GameViewController shareGame] + 327
13 Dodge 0x00107871 -[GameScene runShareGame:] + 129
14 libobjc.A.dylib 0x006057cd -[NSObject performSelector:withObject:withObject:] + 84
15 UIKit 0x01119a90 -[UIApplication sendAction:to:from:forEvent:] + 99
16 UIKit 0x01119a22 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
17 UIKit 0x0125a18a -[UIControl sendAction:to:forEvent:] + 69
18 UIKit 0x0125a5a7 -[UIControl _sendActionsForEvents:withEvent:] + 598
19 UIKit 0x01259811 -[UIControl touchesEnded:withEvent:] + 660
20 UIKit 0x01171cfa -[UIWindow _sendTouchesForEvent:] + 874
21 UIKit 0x011727d6 -[UIWindow sendEvent:] + 792
22 UIKit 0x011306d1 -[UIApplication sendEvent:] + 242
23 UIKit 0x01140b08 _UIApplicationHandleEventFromQueueEvent + 21484
24 UIKit 0x01114337 _UIApplicationHandleEventQueue + 2300
25 CoreFoundation 0x0088806f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
26 CoreFoundation 0x0087db7d __CFRunLoopDoSources0 + 253
27 CoreFoundation 0x0087d0d8 __CFRunLoopRun + 952
28 CoreFoundation 0x0087ca5b CFRunLoopRunSpecific + 443
29 CoreFoundation 0x0087c88b CFRunLoopRunInMode + 123
30 GraphicsServices 0x03d3d2c9 GSEventRunModal + 192
31 GraphicsServices 0x03d3d106 GSEventRun + 104
32 UIKit 0x01118106 UIApplicationMain + 1526
33 Dodge 0x00107e7a main + 138
34 libdyld.dylib 0x030e5ac9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
答案 0 :(得分:0)
我认为问题在于您正在创建一个新 GameViewController,因为您正在这样做,它正在崩溃。看起来它与它无法找到SKView的事实有关,因为它没有从故事板加载。
据说,你不想要一个新的GameViewController你想要使用当前的。有几种方法可以解决这个问题。最简单的可能是......
-(IBAction)runShareGame:(id)sender{
UIViewController *vc = self.view.window.rootViewController;
if ([vc isKindOfClass:[GameViewController class]])
{
GameViewController *myViewController = (GameViewController *)vc;
[myViewController shareGame];
}
}
但是,如果您使用导航控制器或类似的东西,您可能无法获得GameViewController,而是获得UINavigationController。
最好的方法是创建一个委托并在创建场景时分配它。这个问题有很多不同的方法来解决你想要做的事情。 How do I present a UIViewController from SKScene?但是你的应用程序崩溃了,因为你正在重新创建GameViewController,这使你的问题有点不同。一个人确实提到了委托方法,但您将在Scene上创建协议而不是视图控制器。
修改强>
https://stackoverflow.com/a/21917919/851041
这是委托人很好地使用comunicate回到你的View Controller。
希望这很有帮助。