首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS新手- viewDidLoad

iOS新手- viewDidLoad
EN

Stack Overflow用户
提问于 2013-11-15 22:26:52
回答 3查看 368关注 0票数 0

我正在查看Apple Start Developing Guide,在演示应用程序中遇到了一个错误。

代码如下:

代码语言:javascript
复制
#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之后使用;。我已经在谷歌上搜索过了,看看我是否能找出原因,但似乎不能。

EN

回答 3

Stack Overflow用户

发布于 2013-11-15 22:34:08

将其放入您的.h文件中

代码语言:javascript
复制
 @interface className : Class that you inherit

 @property NSMutableArray *toDoItems;

 @end

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

并将其放入您的.m文件中

代码语言:javascript
复制
@implementation className

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

@end

这应该会有帮助。

希望它能有所帮助:)

票数 6
EN

Stack Overflow用户

发布于 2013-11-15 22:31:29

看起来你可能有这样的属性:

代码语言:javascript
复制
@property NSMutableArray *toDoItems;

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

代码语言:javascript
复制
-(void)viewDidLoad{
    ...
}

在头文件(.h)中。

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

票数 0
EN

Stack Overflow用户

发布于 2013-11-15 22:50:46

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

代码语言:javascript
复制
 @property NSMutableArray *toDoItems;

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

代码语言:javascript
复制
- (void)viewDidLoad
 {
[super viewDidLoad];

self.toDoItems = [[NSMutableArray alloc] init];
  [self loadInitialData];
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20003300

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档