-5

I want to read the NSMutableArray *menu from my MenuViewController.h, i am tired of trying. This is my code now:

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@end




//  ViewController.m


#import "ViewController.h"
#import "MenuViewController.h"

@interface ViewController ()

@end

@implementation ViewController

#pragma mark - view Did Load, View Will Appear & didReceiveMemoryWarning

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

}

-(void)viewWillAppear:(BOOL)animated {

        MenuViewController *menuView = [[MenuViewController alloc] init];
    [self.view addSubview:menuView.view]; // this line is new

    NSLog(@"%@", menuView.self.menu);
    [menuView.menu addObject:@"Darommmm"];

    NSLog(@"%@", menuView.menu);

    [menuView.tableView reloadData];


}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

So this is the code of my viewController.m and .h. Below i will show the code of MenuViewController.h and .m

//  MenuViewController.h


#import <UIKit/UIKit.h>

@interface MenuViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *menu;
@property (nonatomic, retain) NSMutableArray *sections;

@end


//  MenuViewController.m

#import "MenuViewController.h"

@interface MenuViewController ()

@end

@implementation MenuViewController

@synthesize menu, sections, tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

#pragma mark - View Did load, View Will Appear & didReceiveMemoryWarning

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



    // set background to transparent + no seperator 
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.backgroundColor = [UIColor clearColor];


    // allocate the mutable array for our menu
    self.menu       = [[NSMutableArray alloc] init];


    // standard menu items to our object
    [self.menu addObject:@"Dashboard"];
    [self.menu addObject:@"Chats"];
    [self.menu addObject:@"Visitors"];
    [self.menu addObject:@"Log Out"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.menu count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (!cell) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [self.menu objectAtIndex:indexPath.row];

    cell.textLabel.textColor = [UIColor whiteColor];
    UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sidebarcell"]];
    [bg setFrame:CGRectMake(0, 0, 161.5, 42.5)];
    // Configure the cell...
    cell.backgroundView = bg;

    return cell;
}

I can't see what i'm doing wrong, when i try to NSLog the menuView.self.menu i get (null).. Hope you guys can help me out! Cheers.

jszumski
  • 7,430
  • 11
  • 40
  • 53
Sefa
  • 103
  • 1
  • 10

2 Answers2

2

MenuViewController's menu variable never appears to be initialized by the time you're using it, so it's just nil. Put an array in there in your init method (and don't set it in your viewDidLoad if you've already initialized it).

Chuck
  • 234,037
  • 30
  • 302
  • 389
1

menu is nil at the time you're adding elements to it. You initialize it in viewDidLoad of MenuViewController, which is called only after you add that controller to a window. You can fix your issue by using this code, however I wouldn't recommend this as a permanent fix.

MenuViewController *menuView = [[MenuViewController alloc] init];
[self.view addSubview:menuView.view] // this line is new

NSLog(@"%@", menuView.self.menu);
[menuView.menu addObject:@"Darommmm"];

NSLog(@"%@", menuView.menu);

[menuView.tableView reloadData];

You also don't need to call self before accessing the properties of a class.

The proper approach here would be to move the initialization of the NSMutableArray to the controller's init method.

jszumski
  • 7,430
  • 11
  • 40
  • 53
  • When i try to use your code i get this error: No visible @interface for 'UIView' declares the selector 'addSubiew:' – Sefa Apr 12 '13 at 17:44
  • Sorry I had a typo, the correct name is `addSubview:` – jszumski Apr 12 '13 at 17:46
  • 2
    User, its best that you try to understand the lesson that is to be gleaned from these answers. You can't rely on them to be bug free. – Jeremy Apr 12 '13 at 17:50
  • EXC_BAD_ACCESS error now.. I contacted you on Twitter, could you respond please :) – Sefa Apr 12 '13 at 17:50
  • Can you update your question to include the modified code? – jszumski Apr 12 '13 at 17:54
  • Ok i did, my post is modified. – Sefa Apr 12 '13 at 17:58
  • You should move the `self.menu = [[NSMutableArray alloc] init];` line to the controller's initialization method. If you still encounter memory issues, please investigate using NSZombies: http://stackoverflow.com/questions/5386160/how-to-enable-nszombie-in-xcode – jszumski Apr 12 '13 at 18:01