How to passing a NSMutableArray to another ViewController class

前端 未结 4 1681
南方客
南方客 2021-01-15 05:23

I have created NSMutale Array in \"HeroListViewController\". I want use it in another viewController which is MapTutorialViewController. I

4条回答
  •  有刺的猬
    2021-01-15 06:14

    I'm assuming that you have a view containing this new view and the hero list view. If that is the case, then you could create a property in the new view like so:

    @property (nonatomic,retain)HeroListViewController *heroListViewController;
    

    and then set it equal to the heroList from the outside:

    newView.heroListViewController = HeroListViewController;
    

    The main problem with your code at the moment is that you're creating a new instance of HeroListViewController by using alloc init, and you're not accessing the same thing. By setting the new view's heroListViewController property, you can get access to the correct viewController.

    Finally, in viewDidLoad of the new view - I'd actually put the code in viewWillAppear:(BOOL)Animated - you can put code to match the arrays.

    Note that this whole way of doing it is messy and could be better done with a singleton class if you need access to an array in multiple places. The above will help you get it working quick, but if you want a really clean fix, go here: http://www.iphonedevsdk.com/forum/iphone-sdk-tutorials/24135-singleton-classes.html

提交回复
热议问题