equivalent of startactivity & finish in ios?

2019-09-17 01:46发布

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

How do I replace viewcontroller instead of push?

1条回答
疯言疯语
2楼-- · 2019-09-17 01:52

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.

查看更多
登录 后发表回答