我有一个应用程序,其中我有一个文本字段,我在其中显示数据库中的值。文本字段旁边有一个按钮。我有选择器,其中包含4个值,即自定义,步行,跑步,慢跑。
如果textfield包含一个值,即如果包含'Walk',那么在点击按钮时我会显示其中填充了4个值的选择器。但我的问题是,当点击按钮时,应该在选择器上设置值'Walk'。
这是我的代码。
- (void)viewDidLoad
{
[super viewDidLoad];
currentarray = [[NSMutableArray alloc] initWithObjects:@"Custom",@"Walk",@"Run",@"Jog",nil];
// Do any additional setup after loading the view from its nib.
}
这是我的按钮操作,单击此按钮将打开选择器:
-(IBAction)choose
{
actionsheet = [[UIActionSheet alloc]initWithTitle:[currentarray objectAtIndex:0] delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil, nil];
[actionsheet setTitle:@"Picker"];
pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 0, 0)];
pickerView.dataSource=self;
pickerView.delegate=self;
pickerView.showsSelectionIndicator=YES;
[actionsheet addSubview:pickerView];
[actionsheet showInView:self.view];
[pickerView selectRow:[txtTextfield.text intValue ]-1 inComponent:0 animated:YES];
[actionsheet showInView:[[UIApplication sharedApplication]keyWindow]];
[actionsheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *string;
string = [NSString stringWithFormat:@"%@",[currentarray objectAtIndex:row]];
return string;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
-(NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component{
return [currentarray count];
}
答案 0 :(得分:0)
您可以像这样设置pickerView,
[pickerView setObject:textField.text];
查看以下链接,