2013-07-24 66 views
0

我跟隨this tutorial,它幫助了許多像我這樣的初學者。 有2個屏幕,播放器(它顯示的球員名單)和PlayerDetails(它顯示一個播放器的詳細信息,也允許添加一個新的)代表在源代碼管理器中發佈

這是代碼

#import <UIKit/UIKit.h> 
#import "PlayerDetailsViewController.h" 

@interface PlayersViewController : UITableViewController<PlayerDetailsViewControllerDelegate> 
@property (nonatomic, strong) NSMutableArray *players; 
@end 

#import "Player.h" 
#import "PlayerCell.h" 
#import "PlayersViewController.h" 

@class PlayerDetailsViewController; 

@interface PlayersViewController() 


@end 

@implementation PlayersViewController 

@synthesize players; 

- (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 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [self.players count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    PlayerCell *cell = (PlayerCell *)[tableView dequeueReusableCellWithIdentifier:@"PlayerCell"]; 
    Player *player = [self.players objectAtIndex:indexPath.row]; 
    cell.nameLabel.text = player.name; 
    cell.gameLabel.text = player.game; 
    cell.ratingImageView.image = [self imageForRating:player.rating]; 
    return cell; 
} 

- (UIImage *)imageForRating:(int)rating 
{ 
    switch (rating) 
    { 
     case 1: return [UIImage imageNamed:@"1StarSmall.png"]; 
     case 2: return [UIImage imageNamed:@"2StarsSmall.png"]; 
     case 3: return [UIImage imageNamed:@"3StarsSmall.png"]; 
     case 4: return [UIImage imageNamed:@"4StarsSmall.png"]; 
     case 5: return [UIImage imageNamed:@"5StarsSmall.png"]; 
    } 
    return nil; 
} 



/* 
// 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) 
    { 
     [self.players removeObjectAtIndex:indexPath.row]; 
     [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade]; 
    } 
} 



/* 
// 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 - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Navigation logic may go here. Create and push another view controller. 
    /* 
    <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; 
    // ... 
    // Pass the selected object to the new view controller. 
    [self.navigationController pushViewController:detailViewController animated:YES]; 
    */ 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if([segue.identifier isEqualToString:@"AddPlayer"]) 
    { 
     UINavigationController *navigationController = segue.destinationViewController; 
     PlayerDetailsViewController *playerDetailsViewController = [[navigationController viewControllers] objectAtIndex:0]; 
     playerDetailsViewController.delegate=self; 
    } 
} 

-(void) playerDetailsViewControllerDidCancel:(PlayerDetailsViewController *)controller 
{ 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 


-(void)playerDetailsViewController: PlayerDetailsViewController *)controller didAddPlayer:(Player *)player 
{ 
    [self.players addObject:player]; 
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:[self.players count] - 1 inSection:0]; 
    [self.tableView insertRowsAtIndexPaths: [NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} 
@end 


#import <UIKit/UIKit.h> 
#import "Player.h" 

@class PlayerDetailsViewController; 

@protocol PlayerDetailsViewControllerDelegate<NSObject> 
-(void)playerDetailsViewControllerDidCancel:(PlayerDetailsViewController*) controller; 
-(void)playerDetailsViewController:(PlayerDetailsViewController *)controller didAddPlayer:(Player *)player; 
@end 

@interface PlayerDetailsViewController : UITableViewController 
@property (strong, nonatomic) IBOutlet UITextField *nameTextField; 
@property (strong, nonatomic) IBOutlet UILabel *detailLabel; 

- (IBAction)cancel:(id)sender; 
- (IBAction)done:(id)sender; 

@property(weak, nonatomic) id<PlayerDetailsViewControllerDelegate> delegate; 
@end 

#import "PlayerDetailsViewController.h" 

@class Player; 

@interface PlayerDetailsViewController() 

@end 

@implementation PlayerDetailsViewController 

- (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 

#pragma mark - Table view delegate 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    if(indexPath.section==0) 
    { 
     [self.nameTextField becomeFirstResponder]; 
    } 
} 



- (IBAction)cancel:(id)sender 
{ 
    [self.delegate playerDetailsViewControllerDidCancel:self]; 
} 

- (IBAction)done:(id)sender 
{ 
    Player *player = [[Player alloc] init]; 
    player.name = self.nameTextField.text; 
    player.game = @"Chess"; 
    player.rating = 1; 
    [self.delegate playerDetailsViewController:self didAddPlayer:player]; 
} 
@end 

問題: 在PlayersViewController.m,在下面的方法:

-(void)playerDetailsViewController:PlayerDetailsViewController *)controller didAddPlayer:(Player *)player 

我收到以下錯誤消息

  1. 預期的方法體
  2. 使用未聲明的標識符球員

的我在做什麼錯?

回答

2

你缺少一個左括號:

-(void)playerDetailsViewController: PlayerDetailsViewController *)controller ... 

應該是:

-(void)playerDetailsViewController:(PlayerDetailsViewController *)controller ... 
+0

謝謝你,從我這麼笨不會注意到它。 – user2611691