so I have a basic game going in Xcode. I started with Xcode's basic game template and built my game mostly inside the 'GameScene.swift'. When I finished with my game I wanted to add a start screen with a play button so I made a new UIViewController and added a button. I control click and dragged the button to the GameViewController and created a Modal Segue. I then wanted the game to go back when the player died. I tried various ways to dismiss the view but none worked. I am new to swift and really need help. Let me know what code/info is needed to find a solution. I have tried everything I have found on the internet. I thought I found a way around it by adding a button and Segue to the GameViewController to the menu but after multiple pushed it clogged the system and slowed to a crawl because none were dismissed. I can provide any code needed.
Inside of GameViewController.swift in the GameViewController class I tried making a function that was called when the game ended.
I tried to both pop and dismiss the view controller. The function was called and a line printed to console but the view remained. The only thing printed to console is 'nil'
class GameViewController: UIViewController {
func end(){
print(navigationController?.viewControllers as Any)
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
}
# Vollan heres the pic
Here is the screenshot of the storyboard.
thanks in advance.
If you pushed the viewController you use self.navigationController?.popViewController(animated: true)
If you presented it modally you use
self.dismiss(self, animated: true)
When its presented from a modal segue you use
self.presentingViewController?.dismiss(animated: true, completion: nil)
I have a modal segue in a storyboard from one view controller to the other. Then I have a button that's connected to an IBAction that just runs
dismiss(animated: true, completion: nil)
Check and redo the connections from the storyboard to the code. Xcode sometimes just looses a connection when making a lot of changes to the code.
Related
I have a project which has been entirely built using UIKit. We are now trying to slowly introduce SwiftUI for any new UI views that we build into the app.
So y current dilemma is that we have a UIViewController which contains a view with a button that needs to present some kind of a pop-up view containing a list of notifications (which I plan to build using List in SwiftUI. For context, here is how the current view looks (built using UIKit). User would then tap on 'Show me...' and be taken to the new SwiftUI view.
So initially I just tried like this:
#objc func showNotifications() {
let controller = UIHostingController(rootView: NotificationContentView())
self.present(controller, animated: true, completion: nil)
}
This obviously presents a modal view which is great, but I want to be able to control the size of this view. The notification screen should be fairly narrow, not virtually full screen.
I wonder how I might go about implementing this.
I'm developing a SpriteKit game with two different scenes (MainMenu.sks and Settings.sks). I also have a UIViewController.
From the MainMenu the user can tap "Settings" which causes the app to load the Settings.sks scene and present it.
It's done using this code:
// Move to Settings.sks
let transition = SKTransition.flipVertical(withDuration: 1.0)
let loadScene = SettingsScene(fileNamed: "SettingsScene")
loadScene?.scaleMode = self.scaleMode
self.view?.presentScene(loadScene!, transition: transition)
From the Settings.sks scene the user can tap on the SKLabelNode "Change username" which causes the app to load the UIViewController by performing a segue.
The UIViewController contains a UITextField and a UIButton. The UIButton takes the user back to MainMenu.sks.
However, when I'm doing so the it's like a new scene of MainMenu is placed upon the other causing the app to sort of "freeze" and not responding to any touch gestures. I can see from the nodeCount that the nodes are doubled.
A workaround is loading the UIViewController directly from the MainMenu.sks (also via a segue). By doing so, the app works just fine.
I'm suspecting the problem to be the way I load Settings.sks.
What am I missing? Maybe unloading the entire SKView before exiting?
Performing a segue will present a new view on top of the stack. By using a segue to return to the main menu, the app will create a new main menu on top of the UIViewController. I can't say for certain why your app freezes. But I would guess that there are some properties or functions that don't work properly when a new main menu is created and presented from your UIViewController.
I recommend unwinding the segue used to reach the UIViewController instead. This way you can return to the previous view and conserve memory.
You'll need to write an IBAction in your destination view, the main menu in this case:
#IBAction func unwindToMainMenu(segue: UIStoryboardSegue) {
}
Then ctrl-drag from your button in the UIViewController to the exit icon in storyboard. You should see an option to use the IBAction unwindToMainMenu that you just wrote. This will create an unwind segue.
A complete guide to unwind segues and multiple examples can be found in this answer: What are Unwind segues for and how do you use them?
I have a button from my create account page that opens up another view controller that has some information in it.
i move it by doing a segue:
self.performSegue(withIdentifier: "sugueToRules", sender: self)
on my other Viewcontroller i have a back button that connects form the storyboard, to the create account one (the precious one)
when i go back i lose all my data that i put on the create account,
so i must open a new instance of it.
how do i keep all my data of it when i go back ?
do i have to save a local file somewhere and load it when i come back ?
i'm using swift 4
Make sure that they are managed by a UINavigationController. Then the back button will do its job just fine (no need for you to 'wire' something -- this is the default behaviour). Right now it appears that you are allocating a new controller each time you go back.
The same applies to a modally presented controller (with a custom close button for example), although you have to dismiss (or use an exit segue) instead of firing up a segue pointing back.
Passing segue to previous screen will creates a new instance of the previous viewcontroller. It it better to be bound in navigation controller and popout the presented screen using when back navigation is not required.
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
you need to use UINavigationController for navigating UIViewController like -
Having a bug when i try to delete viewcontroller2 which is connected to vc1. Part of the view in viewcontroller1 seems to disappear even though i remove all connections. Ran in to this problem before on same project when connections were not right.
The first image is how it is supposed to be with listname and a button in lower corner.
The last image is how the tableview looks like when i try to remove the other vc.
Any idea of how to solve this?
I don't know if this is what you want but try putting this in the function for when vc2 closes:
self.navigationController?.popViewController(animated: false)
dismiss(animated: false, completion: nil)
self.view.removeFromSuperview()
self.view = nil
If you provide some code it will be easier to understand.
I have the following setup:
A UIPageviewController that has two child-ViewControllers.
ViewController 1 contains a UITableView, ViewController 2 holds a camera setup (AVCaptureSession, etc). It's basically a Snapchat like setup.
Swiping between the two works perfectly - swipe left, UITableView is showing, swipe right and the camera shows up again. So far so good.
Now I want to implement a button that turns the page to get to the controller that hold the UITableView programatically. Here's how I do it:
let startingViewController = self.viewControllerAtIndex(0)
let viewControllers: [UIViewController] = [startingViewController!]
pageViewController!.setViewControllers(viewControllers, direction: .Reverse, animated: true, completion: nil)
This works for the most part, as it slides out the UIViewController holding the camera instance and slides in the one holding the UITableView. However, it seems like the view controller that holds the camera session is deallocated, so when I swipe right again, the camera has to be fired up again. Is there any way to turn the page differently, without having the camera session deallocated?
I have tried adding the camera to the PageViewcontroller and adding an "invisible" overlay, but this is not the solution I am looking for. Can someone help out here?
The correct way to do this is using a UIScrollView with multiple subviews and paging enabled. When you press the button, you do not "turn the page", you manipulate/animate the content of the scroll view.
For further reference:
https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/ScrollViewPagingMode/ScrollViewPagingMode.html