How can I change the view controller the default navigation back button takes me to? The back button usually takes you back to the previous view controller. But what if I want it to go back by two view controllers? I mean I want to change the view controller the back button takes me to. I do not prefer creating a custom back button. So is there any other way? Probably an unwind segue linked to the back button or something?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Using if let syntax in switch statement
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
This may just repeat what has been said above, but I solved this by modifying viewDidLoad in the last VewController. "theControllers" is an array of type UIViewController. In my case, I just wanted to go back to the root view controller, so the array just contains the root viewcontroller (first) and the current viewcontroller (last). Then "setViewControllers" replaces the current stack with the one I created.
You can override
willMoveToParentViewController
. This method is called before your view controller is added in or removed from a container view controller (like aUINavigationController
). When it is being added, theparent
parameter contains the container view controller, when it is being removed, theparent
parameter is nil.At this point you can remove (without animation) the second to last view controller in the navigation stack like so :
Objective-C
Swift
You could also remove more than one or add new ones. Just make sure that when you are finished your array contains at least 2 view controllers with the last one unchanged (the last one is the one being removed and it will be removed from the array automatically after this method is called).
Also note than this method can be called more than once with a nil parameter. For example if you try to pop the view controller using the edge swipe but abort in the middle, the method will be called each time you try. Be sure to add additional checks to make sure that you don't remove more view controllers than you want.
You can do this by adding a custom back button like this. Add your custom action handler on tap on Back button on navigation bar.
Probably easiest way to achieve behaviour you want is to use
navigationController.setViewControllers(controllers, animated: true)
.If you want to go 2 controllers back from controller A instead of only one, use this custom segue when presenting A:
Here is nice tutorial how to set custom class for your segue in Interface Builder: http://blog.jimjh.com/a-short-tutorial-on-custom-storyboard-segues.html
If you are presenting controller via code, use this category:
Then just use
self.navigationController!.replaceCurrentViewControllerWith(newController, animated: true)
instead ofself.navigationControllerPushViewController(newController, animated: true)
.It's easy to adjust this code to remove as much controllers as you need.