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.
Related
As in a conventional project Single View add Scene Kit View on View controller, size 250x250. I need create Onboarding page.
In project i add Page View Controller and root View Controller and Content View Controller. I think add Scene Kit View on Content View Controller. I have problem assigning class SCNView on Scene Kit. As far as I know it should be to fill the screen, but I need to do it only for animation pictures.
Thanks for any suggestions! I'm very excited to hear your suggestions.
You can constrain your SKView just like any other view so that it only fills the part of the display that you need.
For example, if using storyboards:
Create a viewController as normal,
Drag and drop a new view onto this controller (Xcode 7 does not have a SKView in the object library, so use a normal view).
Size this view and add constraints to it to put it where you need it on the display.
Select this view and change its 'Custom Class' to be an SKView.
Link this new SKView to an outlet in your code.
Use this outlet to present your SKScene into the SKView.
I am having a hard time understanding why you can put UIViews outside the UIViewController on the storyboard, and what the use case of it might be.
For instance, on the storyboard I can add UIToolbar, UIAcitivtyIndicator and UIProgressView that is outside of the UIViewController. Is this mean there is a way for you to reference those Views that are outside UIViewController and potentially display them somehow either programmatically or embed those like you would do with a ContainerView?
Yes, it absolutely is possible to do what you're describing!
When you add objects that are outside the view controller, they appear in what Apple calls the "Scene Dock". Apple has the following suggested usage for the scene dock:
If a view is not part of the main view hierarchy — such as a pop-up
menu — add the view to the scene dock. In a running app, the system adds
and removes these kind of views from the view hierarchy when they are
opened and closed.
The steps to make this work are below:
Open the storyboard.
Open the utilities area for the workspace window by clicking the
utilities button in the
toolbar.
In the utilities area, select the Object library by clicking the Object Library button in the library bar.
On the storyboard, select the scene to which you will add the extra view.
Drag a view class object from the object library into the the scene dock.
And importantly...
The added view is a part of the view controller. It will be
instantiated along with the rest of the views. You can attach the view
to a property of the view controller that has an IBOutlet. You can add
more than one view to the scene dock.
(These steps were originally copied from here - unfortunately this page seems to have been deleted by Apple at some point).
Basically until now I create a ViewController in the Storyboard, set its class in the identity inspector and implement the connections and behaviour in the class.
Now I have two ViewControllers (ViewControllerFoo and ViewControllerBar), both are visually the same, but the information and the implementations of the actions of the buttons are different. So I want to create a BaseViewController, with the view creation and common implementation and override some methods in the ViewControllerFoo and ViewControllerBar
If possible I'd like to keep both ViewControllers in the StoryBoard in order to create the segues that launches each one of them visually
How can I do this?
Note: I'm starting with iOS development.
View layout is something that works with view not with view controller.
You can create a simple .xib (or view) file and draw your layout here.
After that add this view to controller or specify that view as main view of this controller.
Or you can create a view class and place all elements programmatically.
Just think about storyboard as 'navigation' utility not 'view layout' utility.
Note: too many views on storyboard speed down your computer and look too ugly.
Draw view in xib to reuse it or create custom views classes/clusters to implement interesting UX/UI effects
I've just started playing around with SpriteKit, and this issue pretty much immediately cropped up. If I have a view controller whose view is an SKView, pushing another view controller on top of it (say, a pause menu or an end game menu) and then unwinding back to the view controller with the SKView takes a noticeable amount of time, in my experience on a 4S and a 5, about a second and a half.
You can test this using the default Hello World template. I set it as the root controller of a navigation view and stuck a button in the navigation bar to trigger a push segue to a new view controller. The second view controller contains a button which triggers an unwind segue. When the button gets pressed, it stays highlighted for about a second and a half, and then finally the segue happens, which is incredibly jarring to the user.
I've glanced through the SpriteKit documentation and didn't notice anything written about the proper use of segues, is this just a bug or is it considered bad practice to push new views on top of an SKView? Instead, should I be using SKNodes/SKScenes to present my pause and end game menus, thereby always keeping the SKView on screen?
I had a similar issue and found this helpful. I was planning on using multiple UIViewControllers to navigate to different sections of my game like a menu screen, game over screen etc. but it was painfully slow. After refactoring my app to use a single UIViewController whose view is of type SKView, and creating a new scene for each screen I wanted, it started to work properly. At first I thought that this would be a pain because the controller would end up getting unmanageable, but found that most of logic was incapsulated in each SKScene and the controller was used to present new scenes only.
Also, you should note that adding UIKit controls to the SKView from an SKScene is acceptable. This is what I am doing to avoid having to reinvent the wheel every time I need a UIScrollView, UIButton or any other UIKit control. Of course you will have to remove the UIKit stuff you don't want to share between scenes when you transition.
Could it be possible that the SKView (or the scene) deallocates while the menu storyboard is presented? That would explain the delay when returning to the scene. Set a breakpoint in the corresponding dealloc messages of custom view/scene subclasses or use Instruments to check for the view and scene being among the live objects while the menu is presented.
Another possibility could be textures being removed from memory and then having to be reloaded. There could be an automatism in Sprite Kit, hard to say.
Try changing the way your storyboards are connected, for example have a "main menu" storyboard before the sprite kit view. It would be interesting to see if you get the same effect entering the view (repeatedly) as you do going back to it.
FWIW I only did a few tests with SK and storyboards but haven't noticed such a delay.
I have been working on an iOS application for sometime now, and I think I am not using the View Controllers I have created properly with the storyboard file / scenes I have created for my application.
As it stands, I have two storyboard files, one for the iPhone, and another for the iPad. In the AppDelegate implementation file there is a method called,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
I have been loading a ViewController from within this method to start the load process for my application.
Since I have two storyboard files now, I sourced some conditional code to load a specific storyboard file based on the device that is running the application.
What is the proper way to load the storyboard file?
After I load the story board file do I initialize the root view controller?
Once my root view controller is loaded should it load the elements specified in the root scene?
If I choose to load another scene from the root scene by clicking on a button in the root scene should I dismiss the root scene, and load the new scene, or is the new scene a subclass of the parent scene? What is the proper way to transition between scenes?
Basically I have a separate view controller for every scene in my application, and I have specified a view controller associated with each scene in IB.
Right now I have a button that can be pressed from the beginning / root scene to load another scene which is associated with another view controller. In this new scene I have a done button which segues back to the root view controller.
If I were click these two buttons repeatedly by clicking on the buttons over and over, am I creating new objects of my view controllers or I am reusing the objects that were already created?
I know this is more than one question but I am trying to get a good grasp on view lifecycles, and how view controllers relate to scenes, and the proper way to load the first view controller, which should load the first scene in an application I presume.
What is the proper way to load the storyboard file?
Click on your project in the class navigator. It is the uppermost thing you can click in there, and it has the name of your application on it. Some config files will open right where your source code used to be, and you need to find the one where you can specify a storyboard for different devices. I think it is the second tab from the left, but i cannot check that right now. You just specify a storyboard to use for iphones and a storyboard to use for ipads, and youll be fine.
EDIT: you also need to specify an initailView for both storyboard files. This can be done by simply opening the identity inspector (i think) in storyboards with the view in question selected and checking the box next to "is initial View". Try all four inspectors if my memory fails me. It'll be there, i promise ;)
What is the proper way to transition between scenes?
in most cases, you will use a segue. those thigs are new in storyboard, and they are the bomb. Theres a method you can implement called
-(void)prepareForSegue:(UIStoryboardSegue *) sender:(id)sender
where you can check for the identifier of the segue and then manipulate the destinationViewController-properties directly. Heres a good tutorial:
http://www.appcoda.com/storyboards-ios-tutorial-pass-data-between-view-controller-with-segue/
If you cant use a segue, however, you will need a rootViewController for anything that displays more than a modal view. Basically, rootViewControllers manage a stack of views, and its not your business how they do it. Depending on your UI you might need a NavigationViewController or a TabBarViewController, just google for both and see what suits you best.
Feel free to ask any questions you might have, ill be glad to help you.
Have fun