I have two view controllers. I have navigated from one view to another view by press the button to using below code.
*let secondViewController = self.storyboard!.instantiateViewControllerWithIdentifier("NotificationController") as! NotificationController self.navigationController!.pushViewController(secondViewController, animated: true)*
For the back, I am using bar button on bar button click for back using below code.
self.navigationController?.popViewControllerAnimated(true)
So my problem is if I am going from one view to another view continuously then it added in a stack. I want to only show another view when it is already added to the stack to stop adding it.It only adds one time.
To create a new view controller, select File->New->File and select a Cocoa Touch Class. Choose whether to create it with Swift or Objective-C and inherit from UIViewController . Don't create it with a xib (a separate Interface Builder file), as you will most likely add it to an existing storyboard.
Both are used for different purpose. A UIViewController class manages a ViewContoller which is responsible for actions that happen within that View controller. This class is aware of actions that happen on view controller, like ViewDidLoad, ViewWillApper, ViewDidAppear, ViewWillDisapper, ViewDidDisapper.
To check whether the navigation stack contains a particular type of view controller
, you can use:
if let viewControllers = self.navigationController?.viewControllers { if viewControllers.contains(where: { return $0 is YourViewController }) { //Write your code here } }
To remove a particular controller from navigation stack, you need to make changes to the navigation stack.
Example:
if var viewControllers = self.navigationController?.viewControllers { for controller in viewControllers { if controller is UIViewController { viewControllers.removeElement(controller) self.navigationController?.viewControllers = viewControllers } } }
For swift 4 you can use
if let viewControllers = self.navigationController?.viewControllers { for vc in viewControllers { if vc.isKind(of: YourViewController.classForCoder()) { print("It is in stack") //Your Process } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With