Push To view controller from View is not working in swift - ios

I tried to push to a view controller from a view class using tap gesture action. All the below lines are executed, but the control not pushing to respective view controller. I need to push to that view controller, but not to set the controller as root view,because i need to navigate back to some other navigated view controller. Any one please help me.
let webViewController = R2WebViewViewController()
currentWindow.rootViewController?.navigationController?.pushViewController(webViewController, animated: true)

try this
(currentWindow?.rootViewController as! UINavigationController).pushViewController(webViewController, animated: true)
Your root must be navVC

currentWindow.rootViewController?.navigationController?
Your problem is that your rootViewController is probably an UINavigationController already, so you should do:
let webViewController = R2WebViewViewController()
(currentWindow.rootViewController as? UINavigationController).pushViewController(webViewController, animated: true)

Related

What is different between this when we navigate one view controller to another

What is the difference between this first call:
let next = self.storyboard?.instantiateViewController(withIdentifier: "AFVC") as! AddFileViewController
self.present(next, animated: true, completion: nil)
and this second:
let dashboard = self.storyboard?.instantiateViewController(withIdentifier: "DBVC") as! DashboardViewController
self.navigationController?.pushViewController(dashboard, animated: true)
The first usage will present the new view controller. This presentation normally slides the new controller up from the bottom. If you want to go back, you need to create a button or something similar to dismiss it.
The second usage will use the navigation controller to display (via push which normally slides in from the right) the new view controller. You will automatically get a "< Back" button in the navigation bar. But this will only work if the calling view controller is already embedded in a navigation controller, otherwise self.navigationController is nil.

How to Tabbar Show in Another Sidemenu item?

Why my Tabbar Show in Another Sidemenu item, here is my code
let navigate = storyboard?.instantiateViewController(withIdentifier: "ViewController") as! ViewController
navigationController?.pushViewController(navigate, animated: true)
For achieving this you have to add navigation controller before making root view controller from tabbar like shown in image. Then it will shown to your another controller.

How to programmatically pop a view controller and present a new one

What I am trying to do is present a viewController and when the user touches a button I want to call popViewController and then present a different viewController. But I cannot figure out how to do it.
Some of the things I have tried are call pop and then immediately call present, call present after a delay, pup the pop with a completion block and then in the completion block call present, as well as other ideas.
I think I am over complicating the whole thing, i believe their should be an easy way to do this.
Thanks for any help with this.
CLARIFICATION:
I do this on a touch:
if let vc = UIStoryboard(name: "Listing", bundle: nil).instantiateInitialViewController() as? ListingViewController
{
vc.listing = listing
vc.editListing = forEdit
vc.title = " "
self.navigationController?.pushViewController(vc, animated: true)
}
From that VC when the user touches a button I do this:
self.navigationController?.popViewController(animated: true)
When the VC goes away I want to do something like this:
if let vc = UIStoryboard(name: "CreateListing", bundle: nil).instantiateInitialViewController() as? NewCreateListingViewController
{
vc.bMakeSimiliar = makeSimiliar
vc.listing = listing
vc.editListing = editListing
vc.title = " "
self.navigationController?.pushViewController(vc, animated: true)
}
Basically my user is viewing the first controller which is a listing, from the listing VC he touches a button to add a new listing, so I want to dismiss the listing view controller and present him with the create listing view controller.
When I execute the above code to present the new VC nothing happens. I am doing this immediately after the popViewController.
You don't use pop to present next controller in stack. Pop actually navigates back from the current controller.
To perform what you're trying to achieve you need to call navigationController.pushViewController(viewController: UIViewController, animated: Bool) method.
In order to get back to some previous view controller you use:
navigationController.popViewController(animated: Bool)
In order to get to the root view controller you use:
navigationController.popToRootViewController(animated: Bool)
In order to get to some specific view controller in the stack you use:
popToViewController(viewController: UIViewController, animated: Bool)

back to initial view controller

As you can see i have an initial view controller with label in it "Initial ViewController". In this view controller i use below code to go to selected tab of Tab Bar Controller
let vc1 = sb.instantiateViewController(withIdentifier: "tabbar") as! UITabBarController
vc1.selectedIndex = selected
let controllers = [vc1]
navigationController!.setViewControllers(controllers, animated: true)
In my First View, there's a button "Present VC" which presents "Presented View".
let vc1 = sb.instantiateViewController(withIdentifier: "PresentedVC") as! PresentedVC
present(vc1, animated: true, completion: nil)
Now i want to go back from presented vc to root vc(Initial View Controller). I can do this with:
let viewController = self.storyboard?.instantiateViewController(withIdentifier: "FirstNavigationController") as! FirstNavigationController
UIApplication.shared.keyWindow?.rootViewController = viewController
but this instantiate Initial View Controller every time and views remain in memory. How can i go back to Initial View Controller without instantiating it?
PS: FirstNavigationController is initial Navigation Controller.
At the very beginning you remove the initial view controller by doing this:
navigationController!.setViewControllers(controllers, animated: true)
All the previous viewControllers are gone now and replaced with the tabbar controller and its children.
If you want to go back to the initial controller, you will have to push the tabbar onto the stack instead of replacing everything with it. E.g.
navigationController?.pushViewController(tabbarVc, animated: true)
First when you did this
navigationController!.setViewControllers(controllers, animated: true)
the initial VC is deallocated (if it has not a strong references)
to keep it you can use push , pop instead of setViewControllers but note it may cause memory issues if you have heavy processing in your app as it will remain in navigationController stack
You are missing the concept between "Presenting" or "Setting" View Controller & "Navigating" the View Controller. You will get the answer, once you understood the concept. Here, it is..
When you are setting the ViewController, you are completely replacing the stack container to the new view controller. the way you did it here:
navigationController!.setViewControllers(controllers, animated: true)
In this case, STACK holds the addresses of the latest set/presented ViewControllers that means previous whole ViewController vanished.
On the other hand, if you are navigating to other view controller by pushing it. you can go back to previous controller by simple popping the ViewController address from stack. or to next ViewController by pushing
e.g:
self.navigationController?.pushViewController(tabbarVc, animated: true)
self.navigationController?.popViewController(animated: true)
Summary:
If you push TabBarController then, only you will get InitialVC.
Now, in your case, you are setting the ViewController and hence, you are not getting InivtialVC. Try Pushing tabbarVC This will work.
navigationController?.pushViewController(tabbarVc, animated: true)

Swipe Right to Dismiss ViewControlelr without using Storyboard Swift

How to dismiss any View Controller without using Storyboard ? and do I have to use UINavigationController to achieve this ? if not how then ?
if someone is still struggling to dissmiss on swipe (left,right,bottom,top)
i came across a very elegant and decent cocapod panslip
with a very simple usage
just call this method in viewDidLoad()
For viewController embedded in navigation Controller
let viewControllerVC = self.navigationController
viewControllerVC!.ps.enable(slipDirection: .leftToRight){
//anything you want to do after dissming
}
For Simple View Controller
let viewControllerVC = self
viewControllerVC!.ps.enable(slipDirection: .leftToRight){
//anything you want to do after dissming
}
For TableView embedded in View Controller
let viewControllerVC = self.YourTableview.parentContainerViewController()
viewControllerVC!.ps.enable(slipDirection: .leftToRight){
//anything you want to do after dissminn
}
The way to dismiss a view controller is to call a dismiss method on the presenting view controller. If you presented your child controller from a parent by calling self.present(_:animated:) then you can call self.dismiss(animated:). If you used a navigationController and called navigationController.push(_:animated:) then you need to tell the navigation controller to dismiss with navigationController.popViewController(animated:)
Method names might not be exact, but you should be able to figure it our with autocomplete.
If you want to swipe right is not a dismiss, but I think that what you want it to embed the UIViewController inside a UINavigationController.
For example:
if let vc = UIStoryboard(name: "YourStoryboard", bundle: nil).instantiateViewController(withIdentifier: "YourViewController") as? YourViewController {
let navigation = UINavigationController(rootViewController: vc)
navigationController?.pushViewController(vc, animated: true)
}

Resources