我在我的应用程序中有选择器视图它工作正常但问题是如果我想选择那些首先在选择器视图中的值。我无法选择而不滚动选择器视图。首先我向上或向下滚动之后我可以选择那些价值。不选择滚动选择。 无法通过在选择器视图中滚动来获取值(当用户不与选择器视图交互时我想获取默认选择器视图中选择的值)。
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [ ArrayForIdealCalc count];
}
#pragma data source
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [ArrayForIdealCalc objectAtIndex:row];
// return [arrayname objectAtIndex:row];
// return [Selected_Centers objectAtIndex:row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if ([callCalculate isEqualToString:@"WEIGHTMEASURE"])
{
IdealWeightstr =[ArrayForIdealCalc objectAtIndex:row];
_KiloLbl.text=IdealWeightstr;
[_pickDataForIdealWeight setHidden:YES];
[self.pickDataForIdealWeight reloadAllComponents];
}
这是我的可编辑代码
答案 0 :(得分:2)
答案 1 :(得分:2)
你可以使用
selectRow:0 inComponent:0 animated:NO
或者您可以使用
调用选择器委托方法pickerView:myPickerView didSelectRow:0 inComponent:0
答案 2 :(得分:0)
您可以通过向选择器视图添加点按手势,让用户点按/点击一行选择器视图。这是相同的代码。
[self.pickerview selectRow:0 inComponent:0 animated:YES];
UITapGestureRecognizer *tapToSelect = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerTapped:)];
tapToSelect.delegate = self;
[self.pickerview addGestureRecognizer:tapToSelect];
然后添加以下方法:
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
return true;
}
-(void)pickerTapped:(UIGestureRecognizer *)gestureRecognizer
{
if ([self.pickerview selectedRowInComponent:0] == 0)
{
_Kilolbl.text = selectedRowValue;
}
}
这就是你可以将Picker视图中默认选定行的值填充到你想要的UIControl。如果你的默认选择行例如10,那么你应该使用10来进行pickerTapped方法的比较。 /> 不要忘记将UIGestureRecognizerDelegate添加到视图控制器。