UIView won't cast to SKView

梦想的初衷 提交于 2019-12-11 22:52:16

问题


I've been trying to implement Game Center in my Sprite Kit game, but every time I try to present the leaderboard...

-(void)showLeaderboard:(UIViewController*)gcvc {
    GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];
    if (leaderboardController != NULL)
    {
        leaderboardController.leaderboardIdentifier = @"Game_Leaderboard";
        leaderboardController.viewState = GKGameCenterViewControllerStateLeaderboards;
        leaderboardController.gameCenterDelegate = self;
        UIViewController *vc = self.view.window.rootViewController;
        [vc presentViewController: leaderboardController animated: YES completion:nil];
    }
}

The game crashes on startup with the error:

[UIView setShowsDrawCount:]: unrecognized selector sent to instance 0x178169e40 (lldb)

I've looked up related questions and all of them suggest changing the View's class in IB to SKView class, but no luck. The exception breakpoint stops at the line:

SKView *spriteView = (SKView *)self.view;
spriteView.showsDrawCount = YES;

And the view is clearly of type SKView, but it still says that spriteView is of type UIView. I'm not sure if it should even matter since the game's main view controller is a subclass of UIViewController, which is what is needed to present the Game Center Leaderboard so I have no idea how to fix this error.


回答1:


Well after a lot of tweaking, apparently there were a couple issues with the code. UIViewController *vc = self.view.window.rootViewController; wasn't needed since I was already passing the view controller as a parameter, and instead of following Apples code for creating the SKView, I simply connected an IBOutlet and got rid of SKView *spriteView = (SKView *)self.view; I would have never even thought to change the viewDidLoad method so thanks guys!




回答2:


For what it's worth, I usually obtain the SKView from the main UIViewController like so:

/// Returns the Director's own view, but as a SKView *.
@property (weak, readonly) SKView * skview;

// ...

- (SKView *) skview {
  return (SKView *)self.view;
}


来源:https://stackoverflow.com/questions/23928238/uiview-wont-cast-to-skview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!