NSArray 上的超出范围和无效摘要错误
我正在尝试创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码存在一些问题。我不清楚您是手动在问题中输入此内容还是从实际代码中复制并粘贴:
self.dateforPicker
而不是self.dateForPicker
,您的 ivar 和您的财产之间的大小写存在差异。在 iOS 中,当您声明属性时,编译器将合成一个dateforPicker
ivar,该属性是在viewDidLoad
中设置的,但在其他方法中,您可能会引用dateForPicker
ivar,从未被触及。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:
self.dateforPicker
and notself.dateForPicker
, there is a difference in capitalisation between your ivar and your property. In iOS the compiler will have synthesized adateforPicker
ivar when you declared your property, which was set in yourviewDidLoad
, but in your other methods you may be referring to thedateForPicker
ivar, which is never touched.RootViewController
does not declare that it implements theUIPickerViewDataSource
orUIPickerViewDelegate
protocolstitleForRow
method is wrong - yours begins withpickView
rather thanpickerView
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.