我想从我的MenuViewController.h中读取NSMutableArray *菜单,我厌倦了尝试。这是我现在的代码:
// 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
所以这是我的viewController.m和.h的代码。下面我将展示MenuViewController.h和.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;
}
我无法看到我做错了什么,当我尝试NSLog的menuView.self.menu我得到(null)..希望你们能帮助我!欢呼声。
答案 0 :(得分:2)
MenuViewController的menu
变量在您使用它时似乎永远不会被初始化,所以它只是零。在你的init方法中放入一个数组(如果你已经初始化它,不要在你的viewDidLoad
中设置它。)
答案 1 :(得分:1)
menu
为零。您在viewDidLoad
的{{1}}中初始化它,只有在将该控制器添加到窗口后才会调用它。您可以使用此代码修复您的问题,但我不建议将其作为永久性修复。
MenuViewController
在访问类的属性之前,您也不需要调用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];
。
这里适当的方法是将self
的初始化移动到控制器的NSMutableArray
方法。