I'd like to replace viewcontroller (same as startactivity & finish or window.location.replace)
How do I replace viewcontroller instead of push?
I'd like to replace viewcontroller (same as startactivity & finish or window.location.replace)
How do I replace viewcontroller instead of push?
There's multiple ways to implement this based on your view controller containments. If you have the current view controller on top of the stack of a
UINavigationController
, then below is one solution:You want to replace
view controller n
with another view controller (instance).Declare a delegate on
view controller n
's class, it setview controller n - 1
to conform that delegate. When you pushview controller n
, you setview controller n - 1
to itsdelegate
.When you want to replace
view controller n
, you call thedelegate
method from it, passing necessary parameters to construct the new view controller. Then insideview controller n - 1
, you tellUINavigationController
to pop the current top view controller (view controller n
) and push the new view controller.Another solution is to send notification to UINavigationController to pop and push.
UINavigationController
has several useful methods when you want to change the view stack:setViewControllers
(another one with animation param is also available)popToViewController
Not very flexible, but just one way to do so.