NSArray 上的超出范围和无效摘要错误

发布于 2024-12-06 18:40:11 字数 1515 浏览 0 评论 0原文

我正在尝试创建 UIPickerview,如 Dave Mark 的 iPhone 开发书中所述。我有一个 NSArray ,它在 h 文件中声明为属性,它将存储 UIPickerview 的数据。所以这就是我所拥有的:

在 .h 文件中:

@interface RootViewController : UIViewController {
    NSArray *dateForPicker;
}
@property (nonatomic, retain) NSArray *dateforPicker;
@end

.m 文件 viewDidLoad 方法中(我确实有 @synthesize 用于< code>dateForPicker 属性(位于 .m 文件开头):

NSArray *tempArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
self.dateforPicker = tempArray;
[tempArray release];

UIPickerview 出现时,它会出现 "?"viewDidLoad 方法中使用断点检查 tempArray 和 dateForPicker 的值时,我发现 tempArray 没问题,但 dateForPicker永远不会从 tempArray 获取值。 Xcode 对 dateForPicker 数组显示“无效摘要”,并且将“超出范围”作为五行的值。到底是怎么回事?正如书中所描述的,这应该可行。

这是 UIPickerView 的代码:

#pragma mark - 
#pragma mark picker data source methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [dateforPicker count];
}

#pragma mark picker delegate methods

-(NSString *)pickView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
     forComponent:(NSInteger)component{
    return [dateforPicker objectAtIndex:row];
}
@end

I am trying to create the UIPickerview as described in the iPhone development book by Dave Mark. I have a NSArray which is declared as a property in the h file which will store the data for the UIPickerview. So here is what I have:

in the .h file:

@interface RootViewController : UIViewController {
    NSArray *dateForPicker;
}
@property (nonatomic, retain) NSArray *dateforPicker;
@end

In the .m file viewDidLoad method (I do have @synthesize for thedateForPicker property at the beginning of the .m file):

NSArray *tempArray = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
self.dateforPicker = tempArray;
[tempArray release];

When the UIPickerview comes up, it comes up with "?" in all the rows. So when I used a breakpoint to inspect the values of tempArray and dateForPicker in the viewDidLoad method, I find that the tempArray is fine but the dateForPicker never gets the values from the tempArray. Xcode says "Invalid Summary" for the dateForPicker array and has "out of scope" as the values for the five rows. What is going on? As described by the book, this should work.

Here is the code for the UIPickerView:

#pragma mark - 
#pragma mark picker data source methods

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return [dateforPicker count];
}

#pragma mark picker delegate methods

-(NSString *)pickView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
     forComponent:(NSInteger)component{
    return [dateforPicker objectAtIndex:row];
}
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唠甜嗑 2024-12-13 18:40:11

您的代码存在一些问题。我不清楚您是手动在问题中输入此内容还是从实际代码中复制并粘贴:

  • 您正在设置 self.dateforPicker 而不是 self.dateForPicker,您的 ivar 和您的财产之间的大小写存在差异。在 iOS 中,当您声明属性时,编译器将合成一个 dateforPicker ivar,该属性是在 viewDidLoad 中设置的,但在其他方法中,您可能会引用 dateForPicker ivar,从未被触及。
  • 您的 RootViewController 未声明它实现了 UIPickerViewDataSource 或 UIPickerViewDelegate 协议
  • 您对 titleForRow 方法的声明是错误的 -你的以 pickView 而不是 pickerView 开头,因此不会被调用。

如果组件中的行数正确(您说了多个问号,有多少?),那么看起来数据源已正确连接,但您还需要连接委托,因为这才是实际提供的每行的值。令人困惑的是,数据源仅提供组件数量和每个组件的行数。

Some problems with your code. I'm not clear if you've typed this into the question manually or copied and pasted from your actual code:

  • You are setting self.dateforPicker and not self.dateForPicker, there is a difference in capitalisation between your ivar and your property. In iOS the compiler will have synthesized a dateforPicker ivar when you declared your property, which was set in your viewDidLoad, but in your other methods you may be referring to the dateForPicker ivar, which is never touched.
  • Your RootViewController does not declare that it implements the UIPickerViewDataSource or UIPickerViewDelegate protocols
  • Your declaration of the titleForRow method is wrong - yours begins with pickView rather than pickerView so will not get called.

If you have the correct number of rows in your component (you said multiple question marks, how many?), so it looks like the data source is wired up properly, but you also need to connect the delegate, as this is what actually supplies the values for each row. The datasource, confusingly, only supplies the number of components and the number of rows per component.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文