2017-06-12 62 views
0

我在项目的不同部分实现了多个UIPickerViews。大多数工作正常。然而,最新的有一些很难描述的非常奇怪的行为,但你可以在video I've uploaded to HERE中看到它。简而言之,有2个异常:UIPickerView行在滚动期间消失

  1. UIPickerView第一次显示时,它是空的,并且行看起来几乎为零。如果我再次单击动作按钮(使拾取器视图再次出现),它会正常显示。 (更新:当我注释掉pickerView:rowHeightForComponent:方法时,不会发生此问题)。
  2. 然后更奇怪的部分是滚动选取器视图导致物品消失时,他们滚动(和滚动回来的另一种方式不会使他们重新出现)。

该实现相当简单,与我在其他地方所做的类似,所以我无法弄清楚我做错了什么。数据源使用简单的数组并且日志记录显示它正在正常工作。即使屏幕上没有显示任何值的选取器的初始显示,也会根据NSLog行正确记录这些值。事实上,它与第二次调用的记录完全相同,在该调用中它正确显示值(在滚动使它们消失之前)。

“库”栏按钮将调用下面的行动,以配置为UITextField与UIPickerView,因为它的输入,然后使该文本字段第一响应:

- (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]; 
} 

所有选择器视图的委托和数据实施的源方法是:

- (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]; 
} 

任何想法,为什么这不正常表现?

更新:我也尝试了一个简单的现有文本字段,而不是在飞行中创建一个。它没有帮助。

+0

移动这条线按钮点击动作之外,使libraryPicker一个全局变量UIPickerView * libraryPicker = [[UIPickerView的alloc]在里面];如果您发布完整的.m文件,那么我可以对其进行更正! – dip

+0

猜猜,拾取器实例没有正确渲染,只是让全局并尝试,我相信这将解决您的问题。 – dip

+0

试过了,但不幸的是,它没有帮助。无论如何感谢您的建议。 –

回答

0

这里是我如何解决它(从@dip评论有点启发)...

我的数据源/委托对象是一个单独的对象,而且它没有被明确地保留在任何地方,所以我有现在重新安排了一些东西,使它成为一个属性,并将其分配到viewDidLoad中,以便它继续存在。

我猜UIPickerView只有一个弱引用(或两个弱引用),这当然不足以让它长时间提供数据和委托。

所以现在我有以下(不包括实际代表团和数据源的方法):

@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]; 
}