-5

我想从我的 MenuViewController.h 中读取 NSMutableArray *menu,我厌倦了尝试。这是我现在的代码:

//  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;
}

我看不到我做错了什么,当我尝试对 menuView.self.menu 进行 NSLog 操作时,我得到(null).. 希望你们能帮助我!干杯。

4

2 回答 2

2

MenuViewController 的menu变量在您使用它时似乎从未被初始化,所以它只是 nil。viewDidLoad在您的 init 方法中放入一个数组(如果您已经初始化它,请不要在您的方法中设置它)。

于 2013-04-12T17:39:31.157 回答
1

menu在您向其中添加元素时为零。viewDidLoad您在of 中对其进行初始化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到控制器的init方法。

于 2013-04-12T17:39:45.407 回答