How to add Navigation bar programmatically by clicking the button - ios

I have Login, Signup and Reset viewControllers I have connected the UINavigation controller to login controller but when I go to other view controllers by clicking the button like signup or reset password the destination controller appears without navigation bar. help to include navigation bar programmatically when I click the below button.
#IBAction func signupButton(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "SignUpViewController") as! SignUpViewController
self.present(newViewController, animated: true, completion: nil)
}

For display navigation bar you need to add your SignUpViewController in UINavigationController you can add directly in storyboard By
Selecting SignUpViewController -> Editor Menu -> Embed in -> Navigation Controller
OR
you can add programmatically like this
IBAction func signupButton(_ sender: Any) {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "SignUpViewController") as! SignUpViewController
let naviCon = UINavigationController(rootViewController:newViewController)
self.present(naviCon, animated: true, completion: nil)
}

Related

How to imitate same action for back button as in back button of navigation controller using swift?

I have two navigation controllers as follows:
Navigation controller1 -> View controller1
Tab view Controller -> Navigation controller2 -> View controller2
When I'm in View controller2 and pressed back button, I want to go to View controller1. It is working fine with built in back button in navigation bar, but when I tried to write my own it doesn't work.
I have tried the following statements but they didn't work:
self.dismiss(animated: true, completion: nil)
self.navigationController?.dismiss(animated: true, completion: nil)
navigationController?.popViewController(animated: true)
let story = UIStoryboard(name: "Main", bundle: nil)
let pushVC = story.instantiateViewControllerWithIdentifier("VC1")
let navigation = story.instantiateViewControllerWithIdentifier("Navigation1") as! UINavigationController
navigation.pushViewController(VC1!, animated: true)
if you are using navigationController in the first one only then you can use this to navigate to your ViewController2:
let vc2 = storyboard?.instantiateViewController(identifier: "ViewController2") as! ViewController2
navigationController?.showDetailViewController(vc2, sender: self)
Then if you wanna back from viewController2 to ViewController1 you can create cancel button and connect its action and write this in the action:
#IBAction func buttonCancelAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
But if there is a navigationController in both ViewController1 & ViewController2 then you will use the below:
let VC2 = storyboard?.instantiateViewController(identifier: "ViewController2") as! ViewController2
navigationController?.pushViewController(VC2, animated: true)
and you can pop it (Dismiss it):
#IBAction func buttonCancelAction(_ sender: Any) {
navigationController?.popViewController(animated: true)
}

Moving back to parent view controller or dismissing child view controller

On click of a button, I am navigating from one controller to another. I was initially doing self.present(nextViewController, animated:true, completion:nil) and it was covering the whole window on the device. Since I wanted it to be of the size of my main (presenting) controller, below is the code that I use now to present new controller.
Navigating to NewViewController from Parent Controller:
#IBAction func doneButtonAction(_ sender: Any) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "MyViewID") as! NewViewController
nextViewController.view.frame = self.view.bounds
nextViewController.willMove(toParent: self)
self.view.addSubview(nextViewController.view)
self.addChild(nextViewController)
nextViewController.didMove(toParent: self)
//self.present(nextViewController, animated:true, completion:nil)
}
This works fine and navigates me to NewViewController controller. But now from NewViewController controller, I wish to come back to this parent controller for which my dismiss logic is not working.
Going back from NewViewController to Parent Controller
#IBAction func backButtonAction(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
What am I missing? How do I go back to the parent controller?
What you actually had to do to present over the entire window was to modify modalPresentationStyle property of nextViewController.
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "MyViewID") as! NewViewController
nextViewController.modalPresentationStyle = .fullScreen
present(nextViewController, animated: true)
Add this code to doneButtonAction and present it the old way.

Attempt to present UIImagePickerController on ViewController whose view is not in the window hierarchy

I'm in trouble, after that I'm moving from View Controller to a second View Controller and then switching back to the View Controller and clicking on the UI Image Picker it's not appearing and bringing me back to the second View Controller. On the debugger it's appearing:
Attempt to present on whose view is not in the window hierarchy!
I'm really becoming crazy. My code that is bringing me back from second View Controller to the first View Controller is inside a button and it's below:
#IBAction func profileButton(_ sender: Any) {
let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
webVC.profileLink = linkProfilo
self.present(webVC, animated: false, completion: nil)
}
in second view controller add this variable:
var firstVC: ViewController?
and this setter function to set it
setup(firstVC: ViewController) {
self.firstVC = firstVC
}
in the first view controller, when presenting second view controller pass in a reference to self (first view controller)by calling the setup function
let secondVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SecondViewController") as! ViewController
secondVC.setup(firstVC: self)
self.present(secondVC, animated: false, completion: nil)
now when button is clicked in secondVC,
guard let first = self.firstVC else {return}
first.profileLink = linkProfilo
self.dismiss(animated: false, completion: nil)
I finally found the way for fix all of this:
#IBAction func profileButton(_ sender: Any) {
if let webVC = UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as? ViewController {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
webVC.profileLink = linkProfilo
dismiss(animated: false, completion: nil)
appDelegate.window?.rootViewController!.present(webVC, animated: false, completion: nil)
}
}
it's enough to add the appDelegate sharing method and dismiss before to present the webVC for share the data in the right way and don't cause problems on the first view when presented.

Navigation item disappears with popover

I have created a popup displaying a list in which I send the text to another viewcontroller, but when I close the popup, the Navigation Item disappears, and I have already searched for nothing to solve this problem. The code that sends the selected list item in the popover is this:
Shared.shared.filialNome = self.filiais[indexPath.row].razaoSocial
self.recebeCodigo = self.filiais[indexPath.row].codigo
Shared.shared.codigoFilial = self.recebeCodigo
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "AniversarioViewController") as! AniversarioViewController
self.present(newViewController, animated: true, completion: nil)
Type of following = Modal
I have tried to call Present as Popover but the error persists, it seems that it does not close the VC completely, somebody can help me.
Before opening the popover and
After opening the popover
It seems like, as you are presenting the view controller (and ViewControllers do not have navigation item until we embed it in navigation controller).
So you can embed the same ViewController in navigation controller in storyboard and then get the reference for that navigation controller and present the navigation controller like
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navController = storyBoard.instantiateViewController(withIdentifier: "YourNavigationControllerIdentifier") as! UINavigationController
self.present(navController, animated: true, completion: nil)
Or if you don't want to embed you can do programmatically like
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "AniversarioViewController") as! AniversarioViewController
let navController = UINavigationController.init(rootViewController: newViewController)
self.present(navController, animated: true, completion: nil)

Presenting VC embedded in a NavController from AppDelegate

I'm trying to want to present/push a VC embedded in a NavController from AppDelegate. I previously used this code but somehow it's not working anymore:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let VC = storyboard.instantiateViewControllerWithIdentifier("PendingRequest") as! PendingRequestVC
let navController = UINavigationController.self(rootViewController: VC)
let rootViewController = UIApplication.sharedApplication().keyWindow!.rootViewController
rootViewController!.presentViewController(navController, animated: false, completion: nil)
Other codes open my desired VC but not within a navigation pane. Any guidance would be appreciated.
Calling from AppDelegate after user interacts with push notification.
Edit:
I'm able to present the right VC by using this code:
let mainStoryboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
if let viewController = mainStoryboard.instantiateViewControllerWithIdentifier("PendingNavController") as? UINavigationController {
if let yourViewController = viewController.topViewController as? PendingRequestVC {
//yourViewController.getRequestdata()
}
UIApplication.sharedApplication().keyWindow!.rootViewController = viewController;
}
But this code won't allow me to go back using the Close button on my NavBar.
My structure is as follow:
TabController -> NavController1 -> VC1 -> NavController1a -> VC1a
I'm trying to get to VC1a and be able to use the Closed button to go back to VC1
Add a UIButton in your presented ViewController's View. Following event will be performed by that button. You can dismiss your Navigation Controller this way
#IBAction func dismissViewController(sender: AnyObject) {
self.navigationController.dismissViewControllerAnimated(false, completion:nil);
}

Resources