iOS新手 - viewDidLoad

时间:2013-11-15 14:26:52

标签: ios objective-c

我正在浏览Apple Start Developing Guide并且我在演示应用中遇到了错误。

代码如下:

#import "XYZToDoListViewController.h"
#import "XYZToDoItem.h"

@interface XYZToDoListViewController ()

@property NSMutableArray *toDoItems;
-(void)viewDidLoad{
    [super viewDidLoad];
    self.toDoItems = [[NSMutableArray alloc]init];
    [self loadInitialData];
}

@end

@implementation XYZToDoListViewController
-(void)loadInitialData{
    XYZToDoItem *item1 = [[XYZToDoItem alloc]init];
    item1.itemName = @"Buy Milk";
    [self.toDoItems addObject:item1];

    XYZToDoItem *item2 = [[XYZToDoItem alloc]init];
    item2.itemName = @"Go Shopping";
    [self.toDoItems addObject:item2];

    XYZToDoItem *item3 = [[XYZToDoItem alloc]init];
    item3.itemName = @"Wake Up";
    [self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue
{

}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

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

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

    // Configure the cell...
    XYZToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
    cell.textLabel.text = toDoItem.itemName;
    return cell;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}
@end

在构建它时会出现一个错误,表示在;之后预计viewDidLoad。 我用Google搜索了一下,看看我是否可以找出原因,但似乎无法实现。

3 个答案:

答案 0 :(得分:6)

把它放在你的.h文件中

 @interface className : Class that you inherit

 @property NSMutableArray *toDoItems;

 @end

当然,您需要将className替换为您的类的名称,并使用超类(如NSObject,UIViewController,UIView或任何其他类)替换“您继承的类”。

并将其放在.m文件中

@implementation className

-(void)viewDidLoad{
    [super viewDidLoad];
    self.toDoItems = [[NSMutableArray alloc]init];
    [self loadInitialData]; 
}

@end

应该有所帮助。

希望有所帮助:)

答案 1 :(得分:0)

看起来您可能拥有该属性:

@property NSMutableArray *toDoItems;

在实现文件(.m)或方法中:

-(void)viewDidLoad{
    ...
}

在头文件(.h)中。

此外,您还需要@implementation@interface。也许您应该使用文件中的完整代码更新您的问题。

答案 2 :(得分:0)

以下属性应该是.h文件。因为在.h文件中我们需要声明方法,所以属性将声明应该在.h文件中的方法

 @property NSMutableArray *toDoItems;

以下将在.m文件中,因为在.m文件中我们需要定义方法。

- (void)viewDidLoad
 {
[super viewDidLoad];

self.toDoItems = [[NSMutableArray alloc] init];
  [self loadInitialData];
    }