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.