2013-11-15 20 views
0

我正在經歷Apple Start Developing Guide,我在演示程序中遇到了一個錯誤。iOS新手 - viewDidLoad

的代碼如下:

#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搜索瞭解是否可以找出原因,但似乎無法解決。

+2

你把這段代碼放在頭文件(* .h)嗎? –

+0

您應該在'.m'文件中編寫'-viewDidLoad'方法。 – Bhavin

+0

這是在.m文件 –

回答

0

看起來你可能有兩種屬性:

@property NSMutableArray *toDoItems; 

在實現文件(.M)或方法:

-(void)viewDidLoad{ 
    ... 
} 

在頭文件(.h)中。

此外,你需要一個@implementation@interface。也許你應該用文件中的完整代碼更新你的問題。

6

把這個在您的.h文件中

@interface className : Class that you inherit 

@property NSMutableArray *toDoItems; 

@end 
當然

,你需要用你的名字的類來代替類名,而「類,你繼承」與超類如NSObject的,UIViewController,UIView或任何其他類。

,並把這個在您的.m文件

@implementation className 

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

@end 

,應該幫助。

希望它有幫助:)

0

下面的屬性應該在.h文件中。因爲在.h文件中,我們需要聲明的方法,這樣屬性將宣佈這應該是在.h文件中的方法

@property NSMutableArray *toDoItems; 

並將在下面的.m文件,因爲在.m文件,我們需要定義方法。

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

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

@downvoter,請給予您的意見後downvote ?? –