繁体   English   中英

从SKScene呈现UIViewController

[英]Presenting UIViewController from SKScene

我正在尝试从SKScene提供一个UIViewController,但是,应用程序崩溃了,这是我的代码:

1-:

 UIViewController *vc = self.view.window.rootViewController;
    helpVC = [[HelpViewController alloc]initWithNibName:@"HelpViewController" bundle:nil];
    [vc presentViewController: helpVC animated: YES completion:nil];

2

   helpVC = [[HelpViewController alloc]initWithNibName:@"HelpViewController" bundle:nil];
    SKScene *sks = (SKScene*)helpVC;
    [self.view presentScene:sks];

无论哪种方式,我的应用程序都会崩溃,谢谢您的帮助

因以下原因而崩溃:

2014-02-08 16:38:29.119 BrickRacer[13883:70b] -[UIView presentScene:]: unrecognized selector sent to instance 0x10bb7ea50
2014-02-08 16:38:29.122 BrickRacer[13883:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView presentScene:]: unrecognized selector sent to instance 0x10bb7ea50'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000101fe8795 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000101d4b991 objc_exception_throw + 43
    2   CoreFoundation                      0x0000000102079bad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000101fda09d ___forwarding___ + 973
    4   CoreFoundation                      0x0000000101fd9c48 _CF_forwarding_prep_0 + 120
    5   BrickRacer                          0x000000010000bb6d -[ViewController viewDidLoad] + 317
    6   BrickRacer                          0x000000010000b996 -[HelpViewController viewDidLoad] + 54
    7   UIKit                               0x0000000100971fe4 -[UIViewController loadViewIfRequired] + 562
    8   UIKit                               0x00000001009721bd -[UIViewController view] + 29
    9   UIKit                               0x000000010097f03f -[UIViewController viewControllerForRotation] + 54
    10  UIKit                               0x0000000100977f5f -[UIViewController _visibleView] + 87
    11  UIKit                               0x0000000100bb7f89 -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:animation:] + 4550
    12  UIKit                               0x000000010097c579 -[UIViewController presentViewController:withTransition:completion:] + 4769
    13  BrickRacer                          0x000000010001c6a9 -[Menu help] + 521
    14  BrickRacer                          0x000000010001c43a -[Menu touchesBegan:withEvent:] + 554
    15  SpriteKit                           0x00000001017d8712 -[SKView touchesBegan:withEvent:] + 611
    16  UIKit                               0x00000001008bba84 -[UIWindow _sendTouchesForEvent:] + 300
    17  UIKit                               0x00000001008bc633 -[UIWindow sendEvent:] + 988
    18  UIKit                               0x0000000100895fa2 -[UIApplication sendEvent:] + 211
    19  UIKit                               0x0000000100883d7f _UIApplicationHandleEventQueue + 9549
    20  CoreFoundation                      0x0000000101f77ec1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    21  CoreFoundation                      0x0000000101f77792 __CFRunLoopDoSources0 + 242
    22  CoreFoundation                      0x0000000101f9361f __CFRunLoopRun + 767
    23  CoreFoundation                      0x0000000101f92f33 CFRunLoopRunSpecific + 467
    24  GraphicsServices                    0x0000000102abe3a0 GSEventRunModal + 161
    25  UIKit                               0x0000000100886043 UIApplicationMain + 1010
    26  BrickRacer                          0x000000010000ea93 main + 115
    27  libdyld.dylib                       0x0000000104ab15fd start + 1
    28  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

不要尝试直接从SKScene呈现UIViewController ,这会破坏MVC模式。 SKSceneView一部分, View不应该对ViewController一无所知。

相反,您可以使用NSNotificationCenter通知SKSceneUIViewController它应该提供另一个UIViewController

SKSceneUIViewController

- (void)awakeFromNib {
    [[NSNotificationCenter defaultCenter] 
        addObserver:self
        selector:@selector(goToGameOverViewController:)
        name:@"GoToGameOverViewController"
        object:nil];
}

-(void)goToGameOverViewController:(NSNotification *) notification {
    // Perform a segue or present ViewController directly
    //[self performSegueWithIdentifier:@"GameOverSegue" sender:self];
    HelpViewController *helpVC = [[HelpViewController alloc]initWithNibName:@"HelpViewController" bundle:nil];
    [self presentViewController:helpVC animated: YES completion:nil];
}

- (void) dealloc
{
    // If you don't remove yourself as an observer, the Notification Center
    // will continue to try and send notification objects to the deallocated
    // object.
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

SKScene

- (void)gameOver {
    [[NSNotificationCenter defaultCenter]
         postNotificationName:@"GoToGameOverViewController" object:self];
}

您的问题是双重的:

  1. UIViewController SKScene转换为SKScene
    SKScene *sks = (SKScene*)helpVC;
    强制转换应该在其他场景中进行:例如,在基元之间,UI Foundation和CF类之间或类之间以及它的父级之间进行转换(但不应相反!)。
    长话短说-不在这种情况下(这些类没有共同之处)

  2. 您要求视图呈现场景 ,但改为传递viewController
    [self.view presentScene:sks];
    那么,这里发生了什么?
    presentScene:认为他们有一个SKScene对象,因此向sks发送了一些消息。 sks 无法理解的消息...并崩溃。

如何解决呢?

抓住您的演示视图控制器。
在全局中,或者只是从应用程序委托中获取您的根视图控制器:

UIViewController *rootVC = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootVC presentViewController:yourPresentedVC animated:YES completion:nil];

如果您使用的是标签栏/导航控制器,则应抓住提供的VC。

尝试

UIViewController *vc = [[UIApplication sharedApplication] keyWindow].rootViewController;
helpVC = [[HelpViewController alloc]initWithNibName:@"HelpViewController" bundle:nil];
[vc presentViewController: helpVC animated: YES completion:nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM