Unable to complete the operation while pushing a new controller - swift

I am trying to navigate into a new controller, but I am getting the following error:
error The operation couldn’t be completed. (placesapi error 16.)
I have tried a couple of ways to do it, but the error persists.
Attemp1
self.navigationController?.pushViewController(MainViewController(), animated: true)
Attemp2
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
self.present(mainViewController, animated: true, completion: nil)
The identifier of the controller is set as the image shows:
Solve
After cleaning my project and changing the name of the navigationController as Frankenstein mentioned, the navigation works. Note that cleaning was required.

You need to give the story-board name and not the view-controller name:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let mainViewController = storyBoard.instantiateViewController(withIdentifier: "mainViewControllerID") as! MainViewController
Edit: Make sure your Main.storyboard file has a ViewController whose class is set as MainViewController and identifier is set as "mainViewControllerID"

Related

EXC_BAD_ACCESS when using instantiateViewController

I'm trying to make a Home Screen Quick action to open a specific view controller, when I run the app I get a Thread 1: EXC_BAD_ACCESS (code=261, address=0xdac11530) error on the line shown below. Any ideas as to solve this?
func navigateToMoreDoggosVC() {
let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
let moreDoggosVC = storyBoard.instantiateViewController(withIdentifier: "moreDoggosViewController") //Thread 1: EXC_BAD_ACCESS (code=261, address=0x********)
let navVC = self.window?.rootViewController as? UINavigationController
navVC?.pushViewController(moreDoggosVC, animated: true)
}
if you need any more info i would be happy to edit the question.
You can't push a view controller on launching the app.
You need to set a rootViewController and then make it makeKeyAndVisible.
How can we achieve this see below code:
func navigateToMoreDoggosVC() {
let storyBoard = UIStoryboard.init(name: "Main", bundle: nil)
let moreDoggosVC = storyBoard.instantiateViewController(withIdentifier: "moreDoggosViewController")
window?.rootViewController = UINavigationController(rootViewController: moreDoggosVC)
window?.makeKeyAndVisible()
}
You need to add this code in the SceneDelegate.swift file and call it from the willConnectTo function.

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)

Programmatically Segue Between Views in Different Storyboard Files

I have a view HostViewController in Host.storyboard and, in storyboard I am able to segue to AttendDetailViewController in Main.storyboard. However, I want to do this programmatically as follows:
private func attendDetailViewControllerSegue(event: CAEvent) {
let vc = AttendDetailViewController(nibName: "AttendDetailViewController", bundle: nil)
vc.event = event
navigationController?.pushViewController(vc, animated: true)
}
However, when I run this I get the following error:
'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle ... with name 'AttendDetailViewController''
I have tried every solution on the internet for this problem, and I feel it might have something to do with the Views being in different storyboards.
Any thoughts on how to do this segue without an exception?
You have to get the storyboard from the bundle:
var hostStoryboard = UIStoryboard(name: "Host", bundle: Bundle.main)
Then, instantiate and present the view controller:
let someViewController = hostStoryboard?.instantiateViewController(withIdentifier: "AttendDetailViewController") as? AttendDetailViewController
self.navigationController?.present(attendDetailViewController!, animated: true)
Don't forget to set the AttendDetailViewController's storyboard identifier in the storyboard.
I also recommend you safely unwrap attendDetailViewController before using it:
let someViewController = hostStoryboard?.instantiateViewController(withIdentifier: "AttendDetailViewController") as? AttendDetailViewController
if let vc = attendDetailViewController {
self.navigationController?.present(vc, animated: true)
}
1- to load from xib ( used when there is AttendDetailViewController.xib file in your project )
let vc = AttendDetailViewController(nibName: "AttendDetailViewController", bundle: nil)
2- to load from storyboard ( used when the vc is inside a storyboard )
let vc = UIStoryboard(name: "Host", bundle: nil)!.instantiateViewController(withIdentifier: "vciD") as? AttendDetailViewController
3- load programmatically ( used when the vc layout is created programmatically )
let vc = AttendDetailViewController()

how to call existing navigationcontroller in Swift

I have 3 items in my storyboard. 1. viewController (A) connected to 2. Navigation controller and 3. viewController (B) is NOT connected to anything
All 3 items have restoration identifiers set in the storyboard.
viewController (B) is the initial view controller.
I am trying in B to get the navigation controller that A is attached to, but always returns nil:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("viewControllerA") as! ViewControllerA
print(vc.navigationController?) // always prints nil
why!?
UPDATE#1
I can't declare a UINavigationController like a view controller. I've tried setting navigationcontroller with the id of 'myNavigationController' as storyboardID:
When storyboard, I get this error
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
print(self.navigationController!) // fatal error: unexpectedly found nil while unwrapping an Optional value
I've also tried setting the id in restoration identifier, It bombed earlier at the instantiating line
#ColdLogic, see what hits I get when I search the entire project for that identifier:
Because you never instantiated the navigation controller, you instantiated view controller A. You would want something like this if you want both the nav controller and the view controller to be setup like they are in the storyboard
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("YourNavControllerIdentifier") as! UINavigationController
let vc = navigationController.topViewController as! ViewControllerA
Your code directly instantiates an object of type ViewControllerA. Which, unless you setup logic to do it, does not have a navigation controller by default.
This Worked for me!
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyBoard.instantiateViewController(withIdentifier: "HomeNavigationContoller") as! UINavigationController
let viewController = storyBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
navigationController.pushViewController(messageVC, animated: true)
self.present(navigationController, animated: true, completion: nil)
Use NavigationController identifier to access the NavigationController and ViewController A is already attached to it, so it will automatically get loaded to NavigationController

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)