开发者

How to passing a NSMutableArray to another ViewController class

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

in HeroListViewController.h

MapTutorialViewController *maptutorialcontroller;
NSMutableArray *listData; 

set properties and synthesize them correctly

in HeroListViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    listData = [[NSMutableArray alloc] init];   
    }

 - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *HeroTableViewCell = @"HeroTableViewCell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeroTableViewCell];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:HeroTableViewCell] autorelease];
}
NSManagedObject *oneHero = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSInteger tab = [tabBar.items indexOfObject:tabBar.selectedItem];
switch (tab) {
    case kByName:
        cell.textLabel.text = [oneHero valueForKey:@"name"];
        cell.detailTextLabel.text = [oneHero valueForKey:@"secretIdentity"];
        break;
    case kBySecretIdentity:
        cell.detailTextLabel.text = [oneHero valueForKey:@"name"];
        cell.textLabel.text = [oneHero valueForKey:@"secretIdentity"];
    default:
        break;
}

        [listData addObject: [oneHero valueForKey:@"secretIdentity"]];


        count=[listData count];
                printf("No of items of listData:%u\n", count);


if(maptutorialcontroller==nil){
      maptutorialcontroller= [[M开发者_开发技巧apTutorialViewController    alloc]initWithNibName:@"MapTutorialViewController" bundle:nil];
maptutorialcontroller.secondarray=listData;
}
count=[maptutorialcontroller.secondarray count];
printf("No of items of seconarray :%u\n", count);

return cell;

}

OUTPUTS : No of items of listData:3 No of items of seconarray :3 // both are correct

BUT the the problem I have, when I try to use the secondarray in "MapTutorialViewController" like this, in MapTutorialViewController.h

    HeroListViewController *heroviewcontroller;
  NSMutableArray *secondarray; 

set properties and synthesize them correctly

in MapTutorialViewController.m

 - (void)viewDidLoad 
   {
    heroviewcontroller = [[HeroListViewController alloc]initWithNibName:@"HeroListViewController"  bundle:nil]; 
   self.secondarray=[heroviewcontroller.listData mutableCopy];
   //secondarray= heroviewcontroller.listData;
int count;
count = [secondarray count];
//  
   printf("No of items of secondarray from MapTutorialViewContriller :%u\n", count);
   }

OUTPUT : No of items of secondarray from MapTutorialViewContriller :0

Why it is 0

whats the wrong with my code, please help me


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.


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

You should look into delegation and/or notification.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜