push viewController is not working while debugging in iOS - ios

Sometimes, navigationController?.pushViewController method is not working while debugging. My initial controller (self) has already navigation controller. However, we could not see any error log in output screen. Have you ever encountered this problem ? Is might be a memory problem ?
let controller = storyboard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
self.navigationController?.pushViewController(controller, animated: true)
Environment: iOS 12, Xcode 10.1
Regards,

try
DispatchQueue.main.async {
self.navigationController?.pushViewController(controller, animated: true)
}

Please make sure you have set navigation Controller as initial View controller
Then you can use this piece of code for moving between controllers
let controller = storyboard.instantiateViewController(withIdentifier: "MyViewController") as! MyViewController
self.navigationController?.pushViewController(controller, animated: true)
To get back to the controller using Navigation use
self.navigationController?.popViewController(animated: true)
self.dismiss(animated: true, completion: nil)
And I Hope it is not a memory Problem.

Related

Swift - popViewController after Dismiss

I'm trying to dismiss viewcontroller presented as modal("Present Modally") and then popViewController in navigationController. Here is my storyboard, I want to go from QRScanner VC to Monitoring VC.
Here is how I'm presenting QRScanner VC in Add Device VC:
let storyboard = UIStoryboard(name: StoryboardIDs.MainStoryboard, bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: VCIDs.QRDeviceScannerVC) as! QRDeviceScannerVC
controller.deviceName = deviceNameTxt.text
present(controller, animated: false, completion: nil)
Here is how I'm trying to go back to MonitoringVC:
self?.navigationController?.popViewController(animated:true)?.navigationController?.popViewController(animated: false)
Also tried:
self?.dismiss(animated: true, completion: {
self?.presentingVC?.navigationController?.popViewController(animated: false)
})
It always goes to the Add Device VC instead of Monitoring VC
Use an unwind segue, with the unwind target method located in the Monitoring VC. All the right stuff will just happen as if by magic.
You can try with
self?.dismiss(animated: false, completion:nil)
self?.presentingVC?.navigationController?.popViewController(animated: true)
Just check if you have reference to the navigation controller

Present view controller not working on real device

I have an application that will direct user to another viewController once it is logged in.
DispatchQueue.main.async {
let controller = HomeViewController()
controller.isLoggedIn = self.loggedIn
controller.userRole = self.userRole
controller.username = self.username
let navigationController = UINavigationController(rootViewController: controller)
if #available(iOS 13.0, *) {
navigationController.isModalInPresentation = true
navigationController.modalPresentationStyle = .overFullScreen
} else {
// Fallback on earlier versions
}
print("should present here")
self.present(navigationController, animated: true)
}
Above is the "redirecting" part. This code works well in the simulator, but on the real device it does not work. Any idea how I can solve it? Thanks!
Try this also Set home screen identifier in storyboard "HomeViewController"
In code:
let controller = storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
Using .fullScreen as modalPresentationStyle should fix your issue and gives the default style used on iOS 12 or lower.
if #available(iOS 13.0, *) {
navigationController.modalPresentationStyle = .fullScreen
} else {
// Fallback on earlier versions
}
You need to use storyBoard reference for presenting a viewController. Which you can do by using example of the below attached code:-
let VC1 = self.storyboard.instantiateViewController(withIdentifier:"MyViewController") as! ViewController
`let navController = UINavigationController(rootViewController: VC1)` // Creating a navigation controller with VC1 at the root of the navigation stack.
self.present(navController, animated:true, completion: nil)
thanks for the response but I fixed it by
self.dismiss(animated: true) {
self.present(navigationController, animated: true)
}
apparently it didnt work as there was a error saying there was another view controller being presented ( i missed this error previously )

Swift 4 present view controller black screen

I'm presenting another view controller like this:
func goToScreen(id : String) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: id)
self.present(newViewController, animated: true, completion: nil)
}
The problem is, there is a cc. 1 second delay between the appearance of the new viewcontroller, and in the meantime the app shows an all black screen. Why is that? It looks really ugly
change status of animation to false while move to next view controller this will remove the delay. Delay happening only because of animation: true.
Use below code to solve your issue
self.present(newViewController, animated: false, completion: nil)
Hope this will help you
I was inheriting from UITabBarController instead of UIViewController from the screen I was pushing to.

Swift - Present another view controller with its navigation bar

I have two ViewControllers -- one with storyboard and one without. Both of those view controllers have their own Navigation Bar at the top. Now when I use self.presentViewController(editorViewController, animated: true, completion: nil) my editorViewController comes up but without its Navigation bar.
Any ideas how to fix this?
I fixed the problem using the following code:
let editorViewController = IMGLYMainEditorViewController()
let navEditorViewController: UINavigationController = UINavigationController(rootViewController: editorViewController)
self.presentViewController(navEditorViewController, animated: true, completion: nil)
I just added the navEditorViewController as it made my navigation bar with its items to appear.
Try self.navigationController!.pushViewController(...)
Swift 5+
let destinationNavigationController = self.storyboard!.instantiateViewController(withIdentifier: "nav") as! UINavigationController
destinationNavigationController.modalPresentationStyle = .fullScreen
self.present(destinationNavigationController, animated: true, completion: nil)
Here your navigation bar replaces with new navigation bar.
So for everyone still curious about this problem, given that we already have existing UINavigationController other than the current one:
Swift 3
First, we need to find the UIViewController that we want to present:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewController(withIdentifier: "DestinationViewController") as! DestinationViewController
Next, we're doing the same thing for UINavigationController:
let destinationNavigationController = storyboard.instantiateViewController(withIdentifier: "DestinationNavigationController") as! UINavigationController
Then, we want to bring the DestinationViewController to the top of our destination UINavigationController stack:
destinationNavigationController.pushViewController(destinationViewController, animated: true)
Finally, just present destination UINavigationController:
self.present(destinationNavigationController, animated: true, completion: nil)

How to switch view controllers in swift?

I'm trying to switch from one UIViewController to another using code. Right now I have,
self.presentViewController(ResultViewController(), animated: true, completion: nil)
All this is doing is taking me to a black screen instead of the ResultViewController. Any suggestions?
With Storyboard. Create a swift file (SecondViewController.swift) for the second view controller
and in the appropriate function type this:
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "secondViewController") as! secondViewController
self.navigationController.pushViewController(secondViewController, animated: true)
Without storyboard
let secondViewController = ViewController(nibNameOrNil: NibName, bundleOrNil: nil)
self.presentViewController(secondViewController, animated: true, completion: nil)
You can try this one
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let resultViewController = storyBoard.instantiateViewControllerWithIdentifier("ResultView") as ResultViewController
self.presentViewController(resultViewController, animated:true, completion:nil)
Make sure you set your view controller identifier.
Swift 3
To present a controller modally
let vc = self.storyboard!.instantiateWithIdentifier("SecondViewController")
self.present(vc, animate: true, completion: nil)
To show a controller
self.show(vc, sender: self)
Try this:
let vc = ViewController(nibNameOrNil: yourNibName, bundleOrNil: nil)
self.presentViewController(vc, animated: true, completion: nil)
Hope this helps.. :)
The easiest way to do this would be to create a segue by right clicking on the view you are starting on and dragging the blue line to the result view controller the hit the show segue option. Then set the identifier of the segue to "segue". Now add this code wherever you would like in your view controller:
self.performSegueWithIdentifier("segue", sender: nil)
this will trigger the segue that will go to your resultViewController
The easiest way to switch between screens in iOS is. Add a button on the current screen. Then press control and drag the button to the target screen. Then select push. And when you will tap the button the screen will be switched to the target screen.
Make sure you are using a UINavigationViewController.
Source: ios UINavigationController Tutorial.
Shaba's answer is a good start but requires the following so that you
can control the segue from within your view controller:
override func shouldPerformSegue(withIdentifier identifier: String,
sender: Any?) -> Bool {
// your code here
// return true to execute the segue
// return false to cause the segue to gracefully fail }
Source
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = vc
Or
vc = YourViewController()
UIApplication.shared.keyWindow?.rootViewController = UINavigationController(rootViewController: vc) //If you want to add navigation functionality to that VC

Resources