0

I'm using .XIB and without ARC. I'm passing the value of the NSMultableArray to another view, if I put [self presentModel...], it works, but if I call the AnotherView with a button the value of the NSMultableArray of the AnotherView is null!

AnotherView.h

@interface AnotherViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>{
NSMutableArray *otherAnother;
NSMutableArray *arrayOfTheAnotherView;
}
@property (retain, nonatomic) IBOutlet UITableView *tableView;
@property (retain, nonatomic) NSMutableArray *arrayOfTheAnotherView;

AnotherView.m

- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

otherAnother = [[NSMutableArray alloc]init];
otherAnother = [[NSMutableArray alloc]initWithArray:self.arrayOfTheAnotherView];
//    [otherAnother addObjectsFromArray:arrayOfTheAnotherView]; 
NSLog(@"%@", otherAnother);
NSLog(@"%@", arrayOfTheAnotherView);
NSLog(@"%@", self.arrayOfTheAnotherView);
}

The 3 NSLog's has written "null"

CurrentView.h

@interface CurrentViewController : UIViewController {
NSMutableArray * arrayCurrentView;
AnotherViewController *superAnotherView;
}
@property (retain, nonatomic) AnotherViewController *superAnotherView;

CurrentView.m

@synthesize superAnotherView;
NSString *x = [[NSString alloc]initWithFormat:@"%@",[label text]];

arrayCurrentView = [[NSMutableArray alloc]init];
[arrayCurrentView retain];
[arrayCurrentView addObject:x];

self.superAnotherView = [[AnotherViewController alloc]initWithNibName:nil bundle:nil];
self.superAnotherView.arrayOfTheAnotherView = [[NSMutableArray alloc]init];
[self.superAnotherView.arrayOfTheAnotherView retain];
[self.superAnotherView.arrayOfTheAnotherView addObjectsFromArray:arrayCurrentView];

I don't know how to retain the value of the NSMultableArray, thanks the help.

  • How do you 'call AnotherView with a button'? – Alexander Aug 29 '12 at 14:32
  • It is how I call the AnotherView: UIButton *buttonAnother = [UIButton buttonWithType:UIButtonTypeCustom]; [buttonAnother setTag:5]; [buttonAnother addTarget:self action:@selector(switchTabBar:) forControlEvents:UIControlEventTouchDown]; [tabBarViewController.view addSubview:buttonAnother]; - (IBAction)switchTabBar:(id)sender { switch ([(UIButton *)sender tag]) { case 5: [self.tabBarController setSelectedIndex:0]; break; } – Israel Mesquita Aug 29 '12 at 15:00

1 Answers1

0

There's a few things wrong with the code, like you initialize otherAnother array twice, and viewDidLoad may be firing before you set arrayOfTheAnotherView. There's plenty of ways to do this, but one question. Are you meaning to create copies of the array's or just pointers to the same array object?

It may make things more simple if you create a custom initialize statement for AnotherViewController like this.

- (id) initWithSomeArray: (NSMutableArray *) _arrayOfTheAnotherView{
    self = [super init];
    if (self){
        arrayOfTheAnotherView = [[NSMutableArray alloc] initWithArray:_arrayOfTheAnotherView];
        //Or if you didn't' want a copy
        //arrayOfTheAnotherView = _arrayOfTheAnotheView;

    }
    return self;
}
Augie
  • 1,341
  • 1
  • 11
  • 18
  • Yes, I wanna copy the array, and where I put this code? In the currentView or in the AnotherViewController?? – Israel Mesquita Aug 29 '12 at 15:21
  • the above method "initWithSomeArray:" should copy it. You'll need to add the function to you anotherViewController .m file and declare it in the anotherViewController.h file. Then in currentView.m, you can create a anotherViewController instance like so. self.anotherViewController = [[AnotherViewController alloc] initWithSomeArray: arrayCurrentView]; – Augie Aug 29 '12 at 15:33
  • 2 questions: how Can I call the method (initWithSomeArray) in the anotherViewController.m on the DidLoad? self.superAnotherView.arrayOfTheAnotherView = [[AnotherViewController alloc] initWithSomeArray: arrayCurrentView]; – Israel Mesquita Aug 29 '12 at 15:42
  • From currentView Controller, when you call "self.anotherViewController = [[AnotherViewController alloc] initWithSomeArray: arrayCurrentView]", it allocates a anotherViewController object, and runs the initWithSomeArray function, which creates a copy of the array you pass that call (arrayCurrentView) and places the copy in arrayOfTheAnotherView property. Then viewDidLoad fires automatically, which creates another copy of the array, this time in property otherAnother. Not sure why you need 3 copies of the same array, but looks like that's what you are doing. – Augie Aug 29 '12 at 15:48
  • this may help. http://stackoverflow.com/questions/10684342/initwithnibname-and-viewdidload And based on your goal, my approach may not be what you want. – Augie Aug 29 '12 at 15:51
  • Ignore the first, 2 questions: how Can I call the method (initWithSomeArray) in the anotherViewController.m on the DidLoad? In this case I have to use: the "superAnotherView", because I need access the arrayOfTheAnotherView: self.superAnotherView.arrayOfTheAnotherView = [[AnotherViewController alloc] initWithSomeArray: arrayCurrentView]; well, I put it, and apper a warning: Incompatible pointer types assigning to 'NSMultableArray *' from 'AnotherViewController *' the method is called, and works,but the return is (id) and not NSMultableArray, so self.superAnother.arrayAnother is equals null. – Israel Mesquita Aug 29 '12 at 15:57
  • I don't need 3 copys, I just need, pass a multableArray to another view and load in a tableView, the load of the table view is right, but I want the value of the multableArray, because always return null. – Israel Mesquita Aug 29 '12 at 16:01
  • Maybe, you can help me, Can you give me some email to contact you? – Israel Mesquita Aug 31 '12 at 00:52