Presenting ViewController after resetting NSUSerDefaults - swift

I have a reset button that resets all NSUserDeafults then it reloads to the first game of the NavigationController.
//Reset the NSUserdefault saved objects (No problems here)
let appDomain : NSString = NSBundle.mainBundle().bundleIdentifier!
NSUserDefaults.standardUserDefaults().removePersistentDomainForName(appDomain)
println("button pressed")
//Present first ViewController (Having problems in this code)
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let mainViewController:ViewController = ViewController()
presentViewController(mainViewController, animated: true, completion: nil)

you have to do something like this - but i am having some problems when using this later on... sigh
I wish i knew the correct way to reset the view like this - but i can't find any info.
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var setViewController = mainStoryboard.instantiateViewControllerWithIdentifier("GameViewController") as! UIViewController
var vc = self.view?.window?.rootViewController
vc?.presentViewController(setViewController, animated: true, completion: nil)

Related

Is there an easy way to reset an swift app to clear all variables and seemingly start from the beginning?

At the end of my apps game I want to reset so the game can be played again.
Ive tried the recommended method shown in the code but it causes:
Thread 1: signal SIGABRT error to occur.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
var viewcontrollers = self.navigationController!.viewControllers
viewcontrollers.removeLast()
viewcontrollers.append(vc)
self.navigationController!.setViewControllers(viewcontrollers, animated: true)
Expect to be able to reset the game/app and proceed with a new game by pressing a button.
Can you try this?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
let transition: UIView.AnimationOptions = .transitionFlipFromLeft
let rootviewcontroller: UIWindow = ((UIApplication.shared.delegate?.window)!)!
rootviewcontroller.rootViewController = vc
let mainwindow = (UIApplication.shared.delegate?.window!)!
UIView.transition(with: mainwindow, duration: 0.4, options: transition, animations: nil, completion: nil)
If your (re)starting ViewController class is the rootViewController of your UINavigationController then you can simply call:
navigationController?.popToRootViewController(animated: true)
Try to avoid the bang operator (!) to avoid similar force-unwrap crashes, for example you could safely unwrap the navigationController like so:
if let navController = navigationController {
navController.popToRootViewController(animated: true)
}

Can't instantiate view controller Swift

My code worked well so far. I do not see what I could change to make it bug like that. When I want to return the user to the home page, it works, but a few seconds after the previous ViewController reappears on the screen.
I've tried to change "as! HomeViewController" with "as UIViewController" or as NavigationViewController but it keeps going to the previous ViewController.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.present(balanceViewController, animated: true, completion: nil)
When I want to return the user to the home page...
You should use UINavigationController.popToViewController(_:animated:) to return to a UIViewController:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let balanceViewController = storyBoard.instantiateViewController(withIdentifier: "home") as! HomeViewController
self.navigationController?.popToViewController(balanceViewController, animated: true)

Do I use storyboards right?

Am I using storyboards right?
I have several storyboards with view controllers in them.
When I need to navigate between them, I do the following:
I do create a new instance of the storyboard let storyboard = ... every time I use them. Is this a bad method? Or Should I declare it in the view did load?
func detailsRequestedForMessage(message: message) {
let storyboard = UIStoryboard(name: "Additional", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "MessageDetailsViewController") as! MessageDetailsViewController
viewController.messageId = message.id
self.navigationController?.pushViewController(viewController, animated: true)
}
func viewAllMessage() {
let storyboard = UIStoryboard(name: "Additional", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "AllMessageViewController") as! AllMessageViewController
self.navigationController?.pushViewController(viewController, animated: true)
}
func viewOptions() {
let storyboard = UIStoryboard(name: "Options", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "OptionsViewController") as! OptionsViewController
self.present(viewController, animated: true)
}
It costs nothing to store a storyboard reference, but takes time to resolve a storyboard reference from a name. So I would say that you should put lines like
self.storyboardAdditional = UIStoryboard(name: "Additional", bundle: nil)
in viewDidLoad, storing the references in properties, and thereafter refer to the storyboards by way of the properties.

How to pass data to ViewController in new storyboard

I use this code for go to new story board and pass variable X to a viewController in new storyboard
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerCall")
homeViewController.myVar = "x"
self.present(homeViewController, animated: true, completion: nil)
But I get this error:
Value of type 'UIViewController' has no member 'myVar'
In this line:
let homeViewController: UIViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerCall")
You are saying "Instantiate a view controller from the storyboard, and infer its type to be a UIViewController". However, what you need to do is specifying it as ViewControllerCall.
You can do this by type casting - adding as? ViewControllerCall. (Or as! ViewControllerCall, if you don't want to use the if let clause).
Your code should look like this:
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerCall") as? ViewControllerCall {
homeViewController.myVar = "x"
self.present(homeViewController, animated: true)
}
Or
let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerCall") as! ViewControllerCall
homeViewController.myVar = "x"
self.present(homeViewController, animated: true)
Note that by using as! in the second way I've presented, it will crash your app if the view controller with identifier ViewControllerCall is not of type ViewControllerCall.
You have to change second line code only.
1. Go to your Main storyboard and check the class for ViewController for which you want to pass variable and set storyboard identifier same as class name.
2. Now if your class name is "ViewControllerCall" then change your line code as below:
let homeViewController = mainStoryboard.instantiateViewController(withIdentifier: "ViewControllerCall") as! ViewControllerCall

How to reset/restart viewcontroller in Swift?

I want to have a button on my navigation bar "Reset" and I would like this to be connected to an IBAction to sort of "restart" the controller.
I have some segues from another controller that changes some aspects of the viewcontroller (that has a collectionview) and I want the user to be able to start over. Does anyone have any suggestions as to how to proceed?
If you embed navigation controller to your view controller then you can use this code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewController")
let viewcontrollers = self.navigationController.viewControllers
viewcontrollers.removeLast()
viewcontrollers.append(vc)
self.navigationControllers?.setViewControllers(viewcontrollers, animate: true)
You can change rootViewController of your application:
UIApplication.shared.keyWindow?.rootViewController = UIViewController()
This is how I do it in Swift 2.1 with my Home view inside a UINavigationController:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let homeVC = storyboard.instantiateViewControllerWithIdentifier("HomeViewController")
self.navigationController?.presentViewController(homeVC, animated: false, completion: nil)
self.navigationController?.dismissViewControllerAnimated(false, completion: nil)