How to passing a NSMutableArray to another ViewController class

前端 未结 4 1682
南方客
南方客 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 05:56

    This would only work if you create and fill the mutable array in the init method.

    You should look into delegation and/or notification.

    0 讨论(0)
  • 2021-01-15 06:08

    Example

    firstviewcontroller .h file
    
            before @interface 
    
            use @class secondViewcontroller;
    
    
    declare this inside of @interface  with
    
            secondViewcontroller *sVC;
    
    
    then in firstViewController.m file 
    
            before @implementation          
    
        #import "secondViewcontroller.h"
    
    then
    -------------------
    
    secondVC.h file
    
            @interface inside declare this
    
            say NSMutableArray *secondarray;
    
            and sythasize them.
    
    ------------------- 
    
    after this 
    
            in firstViewcontroller.h viewdidload create this sVC by alloc and initwithnibname then
    
            sVC.secondArray=urfirstArray;
    
            now while u push this sVC controller to navigation controller u can nslog this array in viewdidload.
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-15 06:18

    How is that array being created within HeroListViewController? In this method, you are creating a NEW instance of HeroListViewController and trying to get a property from it. If you already have a HeroListViewController in memory, this is completely wrong.

    Make a property on the class for this viewDidLoad method. It should be of type NSMutableArray. When you allocate and initialize this class, call [set myArray:heroListArray] on it from HeroListViewController. That should give you access to it.

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