How can I add a UIview to a SpriteKit project? - ios

I am working on a game with Sprite kit (swift) and I need a menu with buttons, stats, store etc.
I know I can make it with a SKScene but I want to edit it in the MainStoryBoard not only with code like in SKScene. If I create a new UIViewController file then just segue a button from that view to the SKScene will that be enough? Is it acceptable in terms of efficiency and memory management?

I think that will work fine for you, but it might be harder to work from a new UIViewController file than from SKScene. How effective creating the new UIViewController file versus creating the SKScene in code depends on how comfortable you feel in swift, so its hard to tell which you would prefer. I do not see any problems with memory management, but it could be less efficient than an SKScene because an SKScene was arguably designed for what you are trying to do. In summary, I believe it will be fine for you to use a new UIViewController, but an SKScene is likely the better route.

If you have a view controller in your storyboard (which you probably have if you go with apple's template), add a swift file like you do for any other app and connect it to your view controller.
Add any ui elements you want and add any actions and outlets.
Just make sure your SKScene is being shown IN your view controller. In most cases that is precoded.
SKScene can also be implemented in a UIView, so you can also drag a UIView in a view controller and set it as an SKScene file.
Hope it helps :)

Related

Does it make sense use Storyboard and SceneDelegate together?

Does it make sense to use Application Scene Manifest without SwiftUI? Anyway view controller is added from storyboard ti window already.
I have seen this in an example code, and I am worried it is pointless.

In Swift, is it possible to combine both an SKScene and a UIView in the same ViewController?

The app I am creating has a 'Stats' view controller that incorporates typical UI Elements (Labels, Buttons, ImageViews, etc...), but I also want to be able to use a sprite with gravity on that same screen.
What is the proper way to set this up?
Do I create a View Controller in my storyboard and then link it to a .swift file with a UIViewController class (as normal), but then create a separate class in that same swift file for an SKScene?
OR... if the SKScene class needs to be in a separate .swift file, can I link up both .swift files (UIViewController & SKScene) to that same View Controller in my storyboard?
I have no code to provide (as I don't know where to begin), but if you could point me in the right direction with a term or something to research, I would be most grateful! Enough hours have already been spent looking.

Communication from SKScene to UIViewController

I'm using Google AdMob and to present ads, I do so from the ViewController. Right now, I have a button in my GameScene that is supposed to tell the ViewController to display the ads.
At first, I tried delegation, but that didn't work since SKScene already has a delegate, but then I stumbled upon this answer that showed a way for the SKScene to call a method in its ViewController. The answers seems sketchy, though, as the SKScene holds a reference to its ViewController. My understanding, as well as according to this, was that this is bad practice, assuming that in my case, the SKScene is the View in the MVC structure.
So how should I notify the ViewController of events occurring in the SKScene?
You can check my answer which describes how to communicate SKScene with UIViewController using delegate pattern.

Swift: how to implement additional view controllers with sprite kit game?

Ok, so I'm a beginner here and I am creating a game with Swift in sprite kit.
So far in my storyboard I have the initial View Controller and the Game scene.
I know that my game scene is implemented via the GameScene.swift file, i.e whatever code I write in GameScene.swift affects and correlates to the Game scene.
This is all fine, However I do not understand where I can write code that alters the initial view controller.
I would have assumed that the view controller would be affected by code written in the GameViewController.swift file, however when I try to connect labels and image views from my storyboard to GameViewController.swift by pressing control and dragging, nothing connects. Thus, I have no way of implementing the initial view controller.
Similarly, I cannot implement any additional view controllers that I create in my storyboard.
I can put labels and image views and everything into my view controllers, but I can't connect them to any file to make them do anything.
How do I do this?
It sounds like you have 2 different screens in the storyboard. Each view in a storyboard needs a separate view controller. To connect the ViewController.swift, you need to select the view in the storyboard, and in the identity inspector, make the class the viewController.

What is a ViewController?

I am a newbie, and I am wondering what a ViewController is. I've tried to look at the description, but I find it too complicated. I'm working on Xcode 3 and am trying to link two pages together.
Since you tagged this question with xcode3.2 i'll assume you're asking about UIViewController.
It's one of the UIKit Framework classes.
One of its most important properties is its view (which is an instance of UIView) that usually holds one or more subviews.
What differentiates it from simple UIView are its methods for memory management, handling appearance, implementation in UINavigationController hierarchy...
The dull information on UIViewController can be found here: UIViewController Class Reference
You're definetly want to take a look at View Controller Programming Guide for iOS and View Controller Catalog for iOS.

Resources