Reuse UIViewController instances when using storyboard

后端 未结 1 1606
野性不改
野性不改 2021-02-04 13:47

I decided to give the use of storyboards a go in my current iPhone app. I am facing a bit of a problem. I really need to reuse my UIViewController instances.

What do I

1条回答
  •  孤街浪徒
    2021-02-04 14:30

    If you are using segues, you will need to create custom segues in order to use a cached view controller like you did before. Otherwise, the typical "push" segue will create a new instance of the view controller for segue.destinationViewController. If you write a custom UIStoryboardSegue class and use custom segues you can override initWithIdentifier:source:destination: and put your cached view controller in for the destinationViewController, and then override perform to use the classic pushViewController call.

    That is how you handle the segue if you are really intent on using them. I would just skip it though, unless you really want the fancy arrows to lay everything out on your storyboard. If you skip it you can just instantiate the view controllers into the cache and then push them on just like you did before.

    If your question is more about locating a view controller inside a storyboard then you can use:

    UIViewController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"Some View Controller"];
    

    Then you can save that to your cache and push it like you did in your example code.

    Hope that helps.

    0 讨论(0)
提交回复
热议问题