我想将UIPickerView显示为按钮动作&选择器的选定行的文本将更新为文本字段的文本,以便用户可以使用选择器的选定值在文本字段中手动输入额外的文本。所以,我这样做了:
- (IBAction)commandButton:(id)sender {
commandPicker = [[UIPickerView alloc]init];
commandPicker.delegate = self;
commandPicker.dataSource = self;
[commandPicker sizeToFit];
commandPicker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
commandPicker.showsSelectionIndicator = YES;
[self.view addSubview:commandPicker];
}
但它不起作用。如何将文本字段的文本设置为选择器的选定值?
答案 0 :(得分:0)
您需要从其他视图中显示您的选择器,例如:UIActionSheet:
- (IBAction)commandButton:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"sasdasd" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 470.0)]; //your values
commandPicker = [[UIPickerView alloc]init];
commandPicker.delegate = self;
commandPicker.dataSource = self;
[commandPicker sizeToFit];
commandPicker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
commandPicker.showsSelectionIndicator = YES;
[actionSheet addSubview:commandPicker];
}
对于选择器的设定选择值,使用selectRow:inComponent:animated:
方法。例如:
[commandPicker selectRow:yourTextFieldRow inComponent:0 animated:YES];
答案 1 :(得分:0)
你的方法应该是
- (IBAction)commandButton:(id)sender {
commandPicker = [[UIPickerView alloc]init];
commandPicker.delegate = self;
commandPicker.dataSource = self;
[commandPicker sizeToFit];
commandPicker.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
commandPicker.showsSelectionIndicator = YES;
[self.view addSubView:commandPicker];
}
在你的选择代表
中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(pickerView == commandPicker)
{
NSString *myText = [yourarray objectAtIndex:row];
self.myTextField.text = myText;
}
}
答案 2 :(得分:0)
尝试这种方式来初始化选择器
UIPickerView *commandPicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
commandPicker.delegate = self;
commandPicker.dataSource = self;
commandPicker.showsSelectionIndicator = YES;
[self.view addSubview:commandPicker];
Here is a good reference point on UIPickerview
在deelgate方法中使用didSelectrow方法来查找选择哪一行并从datasource数组中获取值并将其设置为textfield的text属性。
答案 3 :(得分:0)
您最好为选择器视图创建一个出口,连接数据源和委托,试试这个
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.dataArray = [NSMutableArray arrayWithObjects:@"hello",@"happy",@"coding", nil];// set up your array
//initially pickerview is hidden
self.aPickerView.hidden = YES; //initially picker view must be hidden
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
{
return [self.dataArray objectAtIndex:row];
}
- (IBAction)whenButtonTapped:(id)sender
{
if(self.aPickerView.hidden) //it is appearing
{
self.aPickerView.hidden = NO;
}
else
{
self.aPickerView.hidden = YES;
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
//in this method set your text field's text
self.aTextField.text = [self.dataArray objectAtIndex:row];
}