Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hi everyone I have been building an app and have met some problems. My app has two viewControllers. One is MenuViewController and another one is MainViewController.

I want to pass a string from MainViewController to a mutableArray in MenuViewController, but have no idea how.

Here are my codes:

<MenuViewController.h>
#import <UIKit/UIKit.h>

@interface MenuViewController : UITableViewController {
    NSMutableArray *secondFavourite;
}

@property (nonatomic, strong) NSMutableArray *secondFavourite;

@end

.

<MenuViewController.m>
#import "MenuViewController.h"
#import "MainViewController.h"

@interface MenuViewController ()
@property (strong, nonatomic) NSArray *menu;
@end

@implementation MenuViewController
@synthesize menu;
@synthesize secondFavourite;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.secondFavourite = [[NSMutableArray alloc] init];
    self.menu = self.secondFavourite;

}

.

<MainViewController.h>
#import <UIKit/UIKit.h>
#import <social/Social.h>

@interface MainViewController : UIViewController {
    IBOutlet UIImageView *imagepost;
    UILabel *predictionLabel;
}

- (IBAction)sampleSelector:(UIButton *)sender;
- (IBAction)showAllClick:(id)sender;
@property (nonatomic, retain) IBOutlet UILabel *predictionLabel;
@property (strong, nonatomic) NSArray *predictionArray;
@property (strong, nonatomic) UIButton *menuBtn;
@property (strong, nonatomic) NSMutableArray *fav;
@property (strong, nonatomic) IBOutlet UILabel *favLabel;
@property (strong, nonatomic) IBOutlet UITableView* tableView;
@property (nonatomic, strong) NSMutableArray *favourite;
@end

.

<MainViewController.m>

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.predictionArray = [[NSArray alloc] initWithObjects:@"Hey gurl", nil];
}

//Add to favourite

- (void) addToFav {
    self.favourite = [[NSMutableArray alloc] init];
    [self.favourite addObject:self.predictionLabel.text];
    [self.tableView reloadData];
    NSLog(@"%@", self.favourite);
}

//add to favourite button action

- (IBAction)addToFavButton:(id)sender {
    [self addToFav];

//pass data from favourite here to secondFacourite in MenuViewController (found on stack overflow)

    MenuViewController *menuViewController = [[MenuViewController alloc]initWithNibName:@"MenuViewController" bundle:nil];
    menuViewController.secondFavourite = [[NSMutableArray alloc]initWithArray:self.favourite];
    [self.navigationController pushViewController:menuViewController animated:YES];

}

I used NSLog to check that the menuViewController.secondFavourite in MainViewController successfully added the string into the array, isn't the array the same array in MenuViewController? Why doesn't the menu.tableView update and show the new string added? I am very confused and I hope someone will help me out.

Thanks for reading this.

share|improve this question

1 Answer 1

This has to do with the fact that your menu viewDidLoad is overwriting the value in these two lines:

self.secondFavourite = [[NSMutableArray alloc] init];
self.menu = self.secondFavourite;

That first line is setting your secondFavourite property to an empty NSMutableArray instance. And since viewDidLoad will be called only after the view has been loaded into memory (in this case, when you try to push the view controller onto the stack), your initial values in the secondFavourite property will be lost.

Instead, you should move that initialization code into the an init method.

share|improve this answer
    
Thanks @andrew but I'm sorry, how do I move the initialisation code into an init method? Also, my initial values in secondFavourite is empty, and the addToFav method will add object (a string) to secondFavourite. I tried self.secondFavourite = [[NSMutableArray alloc] initWithArray:secondFavourite]; but still doesn't work. –  Henry Ngan May 27 at 7:54
    
@HenryNgan I believe you are having an issue with understanding the order that your code is executing. It would be most beneficial if you put a break point in your MenuViewController's viewDidLoad and inspect the values of the secondFavourite property as you step through. What is happening, is that viewDidLoad is not called until your view controller is being pushed to the navigation stack, so that code happens after you initially set the properties. –  Andrew Monshizadeh May 27 at 14:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.