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) が表示されます。皆さんが私を助けてくれることを願っています! 乾杯。