Ios UIPickerView行在滚动期间消失
我在项目的不同部分实现了几个UIPickerViews。大多数都运作良好。然而,最近的一次有一些非常奇怪的行为,很难描述。简言之,有两种异常:Ios UIPickerView行在滚动期间消失,ios,objective-c,uipickerview,Ios,Objective C,Uipickerview,我在项目的不同部分实现了几个UIPickerViews。大多数都运作良好。然而,最近的一次有一些非常奇怪的行为,很难描述。简言之,有两种异常: 第一次显示UIPickerView时,它是空的,并且行的大小几乎为零。如果我再次单击“操作”按钮(使选择器视图再次出现),它将正常显示。(更新:当我注释掉pickerView:rowHeightForComponent:方法时,不会出现此问题) 更奇怪的是,滚动picker视图会导致项目在滚动时消失(而向后滚动不会使它们重新出现) 实现相当简单,与我在其
pickerView:rowHeightForComponent:
方法时,不会出现此问题)NSLog
行正确记录值。事实上,它的日志与第二次调用的日志完全相同,在第二次调用中,它确实正确地显示了值(在滚动使值消失之前)
“Library”栏按钮调用以下操作,以配置UIPickerView作为输入的UITextField,然后使该文本字段成为第一响应者:
- (IBAction)selectStyleFromLibrary:(id)sender {
_dummyPickerField = [[UITextField alloc] init];
[self.view insertSubview:_dummyPickerField atIndex:0];
StylesController *stylesLibrary = [[StylesController alloc] init];
[stylesLibrary getStyles];
UIPickerView *libraryPicker = [[UIPickerView alloc] init];
libraryPicker.delegate = stylesLibrary;
libraryPicker.dataSource = stylesLibrary;
_dummyPickerField.inputView = libraryPicker;
[_dummyPickerField becomeFirstResponder];
}
实现的picker视图的所有委托和数据源方法如下:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return styleKeys.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return 64;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
NSLog(@"%@", styleKeys[row]);
return styleKeys[row];
}
你知道为什么这不正常吗
更新:我还尝试了一个简单的现有文本字段,而不是动态创建一个。这没有帮助。以下是我如何解决它的(从@dip评论中获得了一些灵感)
我的数据源/委托对象是一个单独的对象,它没有被明确地保留在任何地方,所以我现在重新安排了一些东西,使其成为属性,并在viewDidLoad中分配它,以便它保持不变
我猜UIPickerView对它只有一个弱引用(或两个弱引用),这当然不足以让它长时间地提供数据和授权
现在我有以下内容(不包括实际的委托和数据源方法):
将此行移到按钮单击操作之外,使libraryPicker成为全局变量UIPickerView*libraryPicker=[[UIPickerView alloc]init];如果你发布整个.m文件,那么我可以对其进行更正!我猜,Picker实例没有得到正确的渲染,只需进行全局渲染并尝试,我相信这将解决您的问题。尝试了它,但不幸的是,它没有帮助。无论如何,谢谢你的建议。你能发布你的更新代码吗?为什么不点击按钮而不是文本字段?
@property (nonatomic, strong) StylesController *stylesLibrary;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Metal Tile Very Light"]];
self.editor = (StyleEditor *)self.tabBarController;
if ( ! self.editor.namedStyle ) {
nameField.borderStyle = UITextBorderStyleNone;
nameField.userInteractionEnabled = NO;
}
_stylesLibrary = [[StylesController alloc] init];
[_stylesLibrary getStyles];
}
- (IBAction)selectStyleFromLibrary:(id)sender {
_dummyPickerField = [[UITextField alloc] init];
[self.view insertSubview:_dummyPickerField atIndex:0];
UIPickerView *libraryPicker = [[UIPickerView alloc] init];
libraryPicker.delegate = _stylesLibrary;
libraryPicker.dataSource = _stylesLibrary;
_dummyPickerField.inputView = libraryPicker;
_dummyPickerField.inputAccessoryView = [self accessoryViewWithText:@"Done" target:self action:@selector(libraryPickerDone) forField:_dummyPickerField];
[_dummyPickerField becomeFirstResponder];
}