这是我第一次使用 IB,但在与它相处了一两天后,我相信我开始理解它了。这只是我的说法,我可能在这里忽略了一些简单的事情:
我已经设置了一个 UIPickerView 并将其加入到它在 IB 中的 DataSource 和 Delegate 对象(在我的例子中是两个不同的类)。这允许选择器在我运行应用程序时出现,当它在之前的任何测试运行中都没有出现时,这是非常令人鼓舞的。;) 但是,当我滚动 UIPickerView 时,程序崩溃了,并且我找不到回溯中引用的任何代码。经过相当多的故障排除后,我认为我已经将崩溃范围缩小到两种不同的情况,就回溯而言:
-pickerView:numberOfRowsInComponent的返回值:>显示的行数
- 一旦开始选择新行的动作,应用程序就会崩溃
- 如果我尝试使用 -selectRow:inComponent:animated,应用程序会崩溃:
回溯(忽略主要):
#0 0x955e8688 in objc_msgSend ()
#1 0x0167bea8 in -[UIPickerView table:cellForRow:column:reusing:] ()
#2 0x016773c1 in -[UIPickerView table:cellForRow:column:] ()
#3 0x017fef53 in -[UITable createPreparedCellForRow:column:] ()
#4 0x018077c8 in -[UITable _updateVisibleCellsNow] ()
#5 0x018027cf in -[UITable layoutSubviews] ()
#6 0x03ac42b0 in -[CALayer layoutSublayers] ()
#7 0x03ac406f in CALayerLayoutIfNeeded ()
#8 0x03ac38c6 in CA::Context::commit_transaction ()
#9 0x03ac353a in CA::Transaction::commit ()
#10 0x03acb838 in CA::Transaction::observer_callback ()
#11 0x007b8252 in __CFRunLoopDoObservers ()
#12 0x007b765f in CFRunLoopRunSpecific ()
#13 0x007b6c48 in CFRunLoopRunInMode ()
#14 0x000147ad in GSEventRunModal ()
#15 0x00014872 in GSEventRun ()
#16 0x0168a003 in UIApplicationMain ()
-pickerView:numberOfRowsInComponent的返回值:<显示的行数
- 运动停止并选择行后应用程序崩溃
- 如果我尝试使用 -selectRow:inComponent:animated ,应用程序不会崩溃:
回溯(忽略主要):
#0 0x955e8688 in objc_msgSend ()
#1 0x0167700d in -[UIPickerView _sendSelectionChangedForComponent:] ()
#2 0x017f4187 in -[UIScroller _scrollAnimationEnded] ()
#3 0x016f732c in -[UIAnimator stopAnimation:] ()
#4 0x016f7154 in -[UIAnimator(Static) _advance:] ()
#5 0x00017739 in HeartbeatTimerCallback ()
#6 0x007b7ac0 in CFRunLoopRunSpecific ()
#7 0x007b6c48 in CFRunLoopRunInMode ()
#8 0x000147ad in GSEventRunModal ()
#9 0x00014872 in GSEventRun ()
#10 0x0168a003 in UIApplicationMain ()
我的委托和数据源实现如下:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return (NSInteger)3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return (NSInteger)4;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
//it will probably be better to use the method following when creating the rows, so I can better customize it
return @"strings";
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSLog(@"selected a row");
}