【发布时间】:2009-12-07 10:23:26
【问题描述】:
这是我第一次使用 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");
}
【问题讨论】:
-
你能展示你对 HeartbeatTimerCallback() 的实现吗?此外,您能否将 -didSelectRow 中的 NSLog 更改为 NSLog(@"Selected %d in component %d", row, component) 以便获得预期的索引。还是在那之前就崩溃了?
-
我没有实现 HeartbeatTimerCallback()。我需要吗?回调中列出的方法或函数都不是我的。回复您的其他建议,不,该应用程序永远不会到达 -didSelectRow:inComponent:.
-
我已经弄清楚发生了什么,但我不知道如何正确修复它:我发现如果我覆盖 UIPickerViewDelegate 中的 -dealloc 方法并且故意不发送[super dealloc],应用程序不会崩溃。为什么会这样,使用 IB 处理这种情况的正确方法是什么?
-
你可能发布了一些你没有保留的东西。即超类在其中发布的东西是
-dealloc -
根据 Vladimir 下面的非常有用的回答,IB 正在发布 UIPickerView,正如其设计的那样。我遇到的问题是弄清楚如何正确保留它。恐怕我不理解他所指的文件——或者至少我试图将其翻译成代码的尝试失败了。
标签: iphone interface-builder uipickerview