[英]iOS Novice - viewDidLoad
我正在閱讀《 Apple入門開發指南》 ,但在演示應用程序中遇到錯誤。
代碼如下:
#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上搜索了一下,看看是否可以找出原因,但似乎無法確定。
把它放在你的.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
那應該有所幫助。
希望能幫助到你 :)
看起來您可能具有以下屬性:
@property NSMutableArray *toDoItems;
在實現文件(.m)或方法中:
-(void)viewDidLoad{
...
}
在頭文件(.h)中。
另外,您需要一個@implementation
或@interface
。 也許您應該使用文件中的完整代碼更新問題。
以下屬性應位於.h文件中。 因為在.h文件中我們需要聲明該方法,所以property將聲明應該在.h文件中的方法
@property NSMutableArray *toDoItems;
下面將在.m文件中,因為在.m文件中,我們需要定義方法。
- (void)viewDidLoad
{
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.