2013-10-14 46 views
0

我有一个标签浏览故事板。其中一个选项卡是一个表格视图。该表由动物“标题”列表填充。当按下标题时,会打开一个详细视图,显示除了声音以及动物被点击的次数以外的标题。我有一个视图控制器设置。我也有item.h/m和itemstore.h/m。还有一个详细的控制器。我目前的问题是,在物品存储中,我有两个设置,但离开蝙蝠xcode告诉我,没有找到方法定义。它也给我未申报的标识符错误。我的方法定义没有找到,但我不知道为什么

FSAnimalsViewController.h(这是我的表视图控制器)

#import <AVFoundation/AVAudioPlayer.h> 
#import <UIKit/UIKit.h> 
#import "FSDetailViewController.h" 

@interface FSAnimalsViewController : UITableViewController 
{ 
} 
@end 

FSAnimalsViewController.m

#import "FSAnimalsViewController.h" 
#import "FSItemStore.h" 
#import "FSItem.h" 

@implementation FSAnimalsViewController 
- (id)init 
{ 
    // Call the superclass's designated initializer 
    self = [super initWithStyle:UITableViewStyleGrouped]; 
    if (self) 
    { 
     UINavigationItem *n = [self navigationItem]; 

     [n setTitle:@"FoxSays"]; 

     // Create a new bar button item that will send 
     // addNewItem: to ItemsViewController 

     [[self navigationItem] setLeftBarButtonItem:[self editButtonItem]]; 

    } 
    return self; 
} 

- (void)viewWillAppear:(BOOL)animated 
{ 
    [super viewWillAppear:animated]; 
    [[self tableView] reloadData]; 
} 


- (id)initWithStyle:(UITableViewStyle)style 
{ 
    return [self init]; 
} 


- (void)tableView:(UITableView *)aTableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    FSDetailViewController *detailViewController = [[FSDetailViewController alloc] init]; 

    NSArray *items = [[FSItemStore defaultStore] allItems]; 
    FSItem *selectedItem = [items objectAtIndex:[indexPath row]]; 

    // Give detail view controller a pointer to the item object in row 
    [detailViewController setItem:selectedItem]; 

    // Push it onto the top of the navigation controller's stack 
    [[self navigationController] pushViewController:detailViewController 
              animated:YES]; 
} 


- (NSInteger)tableView:(UITableView *)tableView 
numberOfRowsInSection:(NSInteger)section 
{ 
    return [[[FSItemStore defaultStore] allItems] count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    // Set the cell identifier 
    static NSString *CellIdentifier = @"BasicCell"; 

    // Reuse the cell from the identifier 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; 

    // Configure the cell if it doesn't exist 
    if (!cell) 
    { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    // Log the row for debugging 
    NSLog(@"%d", [indexPath row]); 

    // Get object from store 
    FSItem *item = [[[FSItemStore defaultStore] allItems] objectAtIndex:[indexPath row]]; 

    // Set label to from property in object 
    [[cell textLabel] setText:[item title]]; 

    return cell; 
} 


@end 

FSItem.h

#import <Foundation/Foundation.h> 
#import <AudioToolbox/AudioToolbox.h> 
#import <AVFoundation/AVAudioPlayer.h> 

@interface FSItem : NSObject 

@property (nonatomic, copy) NSString *title; 
@property (nonatomic) SystemSoundID *sound; 
@property (nonatomic) int plays; 


- (NSArray *)animals; 
- (NSArray *)sounds; 

@end 

FSItem.m

#import <AudioToolbox/AudioToolbox.h> 
#import "FSItem.h" 

@implementation FSItem 

NSString *title; 
SystemSoundID *sound; 
int plays; 

- (NSArray *)animals 
{ 
    NSArray *animals = [NSArray arrayWithObjects:@"Dog",@"Cat",@"Bird",@"Mouse",@"Cow",@"Frog",@"Elephant",@"Duck",@"Fish",@"Seal",@"Fox", nil]; 

    return animals; 
} 

- (NSArray *)sounds 
{ 
    NSArray *sounds = [NSArray arrayWithObjects: 
         @"Woof.mp3", 
         @"Meow.mp3", 
         @"tweet.mp3", 
         @"Squeak.mp3", 
         @"Moo.mp3", 
         @"Croak.mp3", 
         @"Toot.mp3", 
         @"Quack.mp3", 
         @"Blub.mp3", 
         @"OWOwOw.mp3", 
         @"Fox.mp3", 
         nil]; 

    return sounds; 
} 

@end 

FSItemStore.h

#import <AVFoundation/AVAudioPlayer.h> 
#import <AudioToolbox/AudioToolbox.h> 
#import <Foundation/Foundation.h> 
@class FSItem; 


@interface FSItemStore : NSObject 
{ 
    NSMutableArray *allItems; 
} 

@property (nonatomic) int i; 





+ (FSItemStore *)defaultStore; 


- (NSArray *)allItems; 
- (NSArray *)animals; 
- (NSArray *)sounds; 
- (FSItem *)createItem; 


@end 

FSItemStore.m

FSItemStore是我的问题,似乎是。它说,没有找到声音和动物的方法定义,声音和动物都是未声明的标识符。任何人有任何想法?

回答

2

你的问题是,在你的H文件中,你声明你的类将实现一个叫做动物的方法,它将返回一个NSArray,并且方法调用将会返回另一个NSArray的声音,但是在你没有实现的M文件中这些方法。

在你FSItemStore.m你应该实现以下方法:

- (NSArray *)animals{ 
    //Do whatever this method is supposed to do, return an NSArray. 
} 
- (NSArray *)sounds 
{ 
    //Do whatever this method is supposed to do, return an NSArray. 
} 

编辑

如果你假装那是什么FSItemStore继承FSItem的方法,您必须声明的接口方式:

@interface FSItemStore : FSItem //FSItem instead of NSObject 
{ 
    NSMutableArray *allItems; 
} 
+0

是不是在FSItem.m照顾? –

+0

@ScottPfeiffer完全没有。如果你假装FSItemStore继承FSItem的方法,你应该按照我在编辑中解释的方式声明它的接口。 –

0

如果我正确理解你的代码,你想设置一个动物和声音initializ在FSItem :: sounds和FSItem :: animals中编辑您在FSItemStore :: createItem中创建的新项目。所以动物和声音方法应该在正确的对象上执行 - FSItem对象。在FSItemStore代码:: createItem改成这样 -

- (FSItem *)createItem 
{ 
    FSItem *item = [[FSItem alloc] init]; 

    if (i < [animals count]) 
    { 
     [item setTitle: [[item animals] objectAtIndex: i]]; 
     [item setSound: [[item sounds] objectAtIndex: i]]; 
     [item setPlays: 0]; 
     i++; 

     [allItems addObject: item]; 
    } 

    return item; 

} 

这仍然是做你想要什么的好方法,因为NSArray的将被初始化每次创建一个项目。如果声音和动物的数量是固定的,那么更好地定义它们以便它们刚刚被初始化,例如, FSItem中的静态对象或FSItemStore上的属性

+0

要挑选您的大脑,以便我除了进一步阐明之外还有更好的理解,应用程序的最终目标是让用户能够记录并提交自己的声音。这不意味着,一旦我实现解析它将不得不重新创建数组以获取所有新用户提交的信息? –

+0

FSItem中的动物和声音的方法让我相信,用户可以从中挑选11种动物和声音的固定数量。在这种情况下,最好将数组初始化一次,并在代码中的任何地方使用相同的数组。但是如果用户实际上可以添加动物和声音的数量,那么你是对的,每次添加新声音时都必须重新创建阵列。但是,你所拥有的代码将被限制为11种声音,因此需要进行更改。 – Kedar

0

您尚未定义任何称为动物或项目的属性或变量。您只定义了getter方法。

例如

@property(nonatomic,strong) NSArray *animals; 
@property(nonatomic,strong) NSArray *items; 

然后执行你的getters,会返回这些属性。

+0

试过这个。它摆脱了方法定义的错误,但动物仍然未申报(你的意思是_动物)。也声音现在给我“一个Objective-C指针隐式转换为'SystemSoundID'(又名无符号长')不允许与ARC。不太确定我能做些什么来解决这个问题。 –

相关问题