Open SKScene from storyboard - interface-builder

From the Main.storyboard generated when making a new SpriteKit iOS game, I made a simple main menu with a play game button using Interface Builder. However, I am trying to make it so when that button is pressed it opens a new SKScene, which I don't want to build with Interface Builder. I have it so when the button is pressed it opens that scene, and the scene has functionality, but the menu GUI remains over the scene, and I want to just clear the Interface Builder menu and have a clear SKScene. I've tried making the storyboard lead to a new view controller to present the scene, and that resulted in a handful of errors, which I did not understand; it stopped and just said (lldb) in the log. I am not sure if I can link a new storyboard view to the scene or just use
#IBAction func onPlayPressed(sender: UIButton) {
println("play pressed")
(self.view as SKView).presentScene(GameScene(size: self.view.bounds.size))
}
to call clear GUI and present the scene
I am new to Xcode and swift so if i apologize if this is a simple question, but I could not find any information on how to do this.
Thanks for any help

After trying a few different things, I was able to make it enter a new blank view controller from the storyboard which the button led to, then clear the storyboard view and present the scene. In the second view controller I added
self.dismissViewControllerAnimated(true, completion: nil)
before presenting the SKScene

Related

How to set a SwiftUI View as a first responder manually?

I'm building an app using mostly SwiftUI, but I have to use a UIKit ViewController to interface with PencilKit. I'm using UIViewControllerRepresentable to interface between them.
So I have a ViewController which I'm using to set up the PencilKit part of my app, and inside it I have to set a PKToolPicker which depends on a UIResponder. To do so I have to write the following code:
canvasView = PKCanvasView(frame: view.bounds)
toolPicker.setVisible(true, forFirstResponder: canvasView)
canvasView.becomeFirstResponder()
This canvasView view I use here is the view which controls the drawing area, and you usually have to set it as a subview of the aforementioned ViewController. The issue is that in my case, this ViewController is nested inside a UIViewControllerRepresentable which is nested inside a SwiftUI View. So, I need to add this SwiftUI view as a second FirstResponder or else the PKTool disappears each time I run a gesture on an SwiftUI view, as canvasView stops being first responder.
So my question is how do I do this? How can I set up a SwiftUI view as a first responder? Couldn't find how to obtain a SwiftUI's UIResponder object though so I could add it as a FirstResponder, anyone know how to?
I'm not really clear about your particular problem, but I assumed that in your UIViewControllerRepresentable you have also
canvasView.becomeFirstResponder()
just before your
toolPicker.setVisible(true, forFirstResponder: canvasView)

How to change the segue type of the storyboards

I've got this problem. I want the view controller to change when the player presses a specific button.
So I added a new "view controller" in the main storyboard, I customised it and then, holding the right button of the mouse, I dragged the button on the new view controller.
It appeared a little menu with different items:
show
show detail
present modally
present as a popover
custom
I've tried all of them, nothing changed (except for the last one: the app crashes).
The new view controller is like a moving window, if you swipe down you go back to the start menu.
What I want is that the new window replaces the first one.
Here's the problem
As you can see, the brown window is movable
As I understand you need to present view controller full screen.
You need to select "present modally" segue type. Then select segue in storyboard
And then select presentation style "Full screen"
Or you can make this programmatically:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let nextVC = storyboard.instantiateViewController(withIdentifier: "nextVC") as! NextVCClass
nextVC.modalPresentationStyle = .fullScreen
self.present(nextVC, animated: true)

Multiple UIViewControllers in a tvOS Storyboard Scene

I'm trying to create a storyboard scene for tvOS where a UIViewController has an embedded UITableViewController. I'm unable to make the connection, though, and I'm not sure if I'm doing something wrong.
At present, I have a UIViewController scene, and a separate UITableViewController scene. The UIViewController scene has a UITableView within it. When I try to make an "embed" connection from the UIViewController to the UITableViewController, I can't select anything else in the Document Outline. I tried adding another UITableViewController to the main scene's dock, and it will only let me drop it onto the canvas, not as a part of the main scene.
I haven't done this with iOS yet, so I'm not sure if what I'm trying is supposed to work, and it's a bug I need to report, or if I'm taking the wrong approach.
Embed segues can't be created from a "normal" UIView, you need to set it from a ContainerView class that you need to drop into your UIViewController.

Previously rendered IBDesignable hangs on "Updating" in storyboard

Xcode 7.0.1 (7A1001)
iOS 9.0
I made a custom #IBDesignable UIView, only implementing the drawRect function. Like so:
#IBDesignable
class CustomView: UIView {
override func drawRect(rect: CGRect) {
let context = UIGraphicsGetCurrentContext()
// Lots more drawing code after this
// ...
}
}
While I was writing this drawRect, I had a storyboard with several different instances of this rendering on a view controller. When I made a change in drawRect and saved, these changes would be reflected in the storyboard in close to real time. There was even an IBInspectable String which allowed me to change an enum type and draw differently depending on that. The views could be resized in the storyboard, and the new width and height would be accounted for in my drawing code, and the drawing would be scaled and translated, exactly as I intended.
This was all working fine, and I had not made any changes to that UIView subclass, but I did delete my test views in the storyboard while building out newer views which would actually end up using this custom view.
After doing a lot of dancing around with constraints, I was ready to place my custom view where it belonged. But it does not work. While the identity inspector acknowledges my custom class as an IBDesignable, it hangs on "Updating" status.
To prove I wasn't going crazy, I made entirely new #IBDesignable UIView subclasses which implemented simple drawRects or even completely empty drawRects. These are now having the same issue and are not rendering on the storyboard.
Note that I can run this in the simulator and the drawing appears as I expect at runtime. So what happened to the storyboard's ability to give that nice feedback I expect from an IBDesignable?
Things I have tried:
Clean / clean build folder / clearing XCode Derived Data directory
Restarting XCode
Deleting / commenting out drawRect guts
Making the most simple #IBDesignables with HelloWorld-like tutorials
Using existing known working #IBDesignables
Creating fresh UIViewController in storyboard to place custom view
Toggled: Editor > Automatically Refresh Views
Editor > Refresh All Views
Editor > Debug Selected Views
All these attempts continue to show "Updating" in Custom Class section of the identity inspector.
Clearly something beyond my implementation is causing a problem here. Anybody else having this issue? Can anybody point me in the right direction? Not sure what else to try.
In the Identity inspector where it says "Module" add your AppName

Linking NSMenu to ViewController - Swift

I am working on a Yosemite app in swift and have hit a road block.
I have multiple views working properly, and now I want to implement custom menu actions. To keep the answer simple, how would I achieve this example. I want to click a menu button and have it change text on the viewcontroller. I have tried setting up IBActions, but I'm not sure how to make the link to the viewcontroller from the AppDelegate. How do you connect the two?
I'm still figuring this stuff out, so any insight would be awesome. Thanks in advance.
*UPDATE. I tried making a object and linking it that way. No luck.
When you press "Test" it prints test, however it's in it's own class. I need to do something in my main ViewController class. How to I make that reference?
Getting NSViewController from NSWindow is an easy solution.
If your app has multiple windows, select appropriate one through keyWindow or windows of NSApplication.
#IBAction func pressed(sender: AnyObject) {
if let window = NSApplication.sharedApplication().mainWindow {
if let viewController = window.contentViewController as? YourViewController {
// do stuff
...
}
}
}

Resources