equivalent of startactivity & finish in ios?

半腔热情 提交于 2020-01-06 14:46:51

问题


I'd like to replace viewcontroller (same as startactivity & finish or window.location.replace)

How do I replace viewcontroller instead of push?


回答1:


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:

UINavigationController
  -- root view controller
  -- view controller 2
  -- view controller 3
  -- other view controllers in stack
  -- view controller n - 1
  -- view controller n

You want to replace view controller n with another view controller (instance).

Declare a delegate on view controller n's class, it set view controller n - 1 to conform that delegate. When you push view controller n, you set view controller n - 1 to its delegate.

When you want to replace view controller n, you call the delegate method from it, passing necessary parameters to construct the new view controller. Then inside view controller n - 1, you tell UINavigationController 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.



来源:https://stackoverflow.com/questions/26558635/equivalent-of-startactivity-finish-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!