Easy way to push and pop UIViewController in swift 5
Step 1 :- Create New file with Name “ExteantionUIViewController.swift”
Step 2 :- Copy and Paste this below extension file
import UIKit
let Application_Delegate = UIApplication.shared.delegate as! AppDelegate
extension UIViewController {
func popVC(_ animation : Bool = true) {
self.navigationController?.popViewController(animated: animation)
}
func push(_ viewController : UIViewController, animation : Bool = true) {
self.navigationController?.pushViewController(viewController, animated: animation)
}
func goBackToViewController(ViewController : UIViewController.Type,isAnimated : Bool = true)-> Bool
{
var isVCAvalabel = false
for controller in self.navigationController!.viewControllers as Array
{
if controller.isKind(of: ViewController)
{
self.navigationController!.popToViewController(controller, animated: isAnimated)
isVCAvalabel = true
break
}
}
return isVCAvalabel
}
func MackAsRootview(_ viewController : viewController)
{
let controller = getVC(on: self.rawValue, viewController)
Application_Delegate.navigationController = UINavigationController(rootViewController: controller)
Application_Delegate.navigationController.navigationBar.isHidden = true
Application_Delegate.navigationController.interactivePopGestureRecognizer!.isEnabled = false;
Application_Delegate.window!.rootViewController = Application_Delegate.navigationController
Application_Delegate.window!.makeKeyAndVisible()
Application_Delegate.window!.bringSubviewToFront(Application_Delegate.objCustomTabBar)
Application_Delegate.objCustomTabBar.isHidden = true
}
}
}
enum myStoryborad : String{
case Storyborad1 = "Storyborad1”, Storyborad2 = "Storyborad2”
func viewController(_ viewController : viewController)->UIViewController{
return getVC(on: self.rawValue, viewController)
}
func pushVC(_ viewController : viewController, animation : Bool = true) {
let controller = getVC(on: self.rawValue, viewController)
Application_Delegate.navigationController.pushViewController(controller, animated: animation)
}
private func getVC(on storyboradName : String, _ viewController : viewController)->UIViewController {
let storyborad = UIStoryboard.init(name: storyboradName, bundle: nil)
return storyborad.instantiateViewController(withIdentifier: viewController.rawValue)
}
}
enum viewController : String{
case LoginVC = "LoginVC", SignupVC = "SignupVC", ForgotPasswordVC = "ForgotPasswordVC"
}
Step 3 :- Access link
- Push VC :-
myStoryborad.Storyborad1.pushVC(.LoginVC)
- Push VC with data :-
let controller = myStoryborad.Storyborad1.viewController(.ForgotPasswordVC) as! ForgotPasswordVC
controller.dictionary = yourDictionary
self.push(controller)
- Popup VC :-
popVC()
nice
ReplyDeleteIt's really nice code
ReplyDelete