我在工具栏中创建了带有完成按钮的UIPickerView。我想完成以下任务:
用户在UIPickerView中选择一个类别,然后点击"完成"按钮。结果应该是UIPickerView和工具栏消失,并且类别TextField会根据用户的选择进行更新。我几乎把一切都搞定了。点击完成按钮后,PickerView和工具栏消失。但是,在点击按钮之前,Textfield会更新。但我希望仅在完成按钮时更新文本字段。
有人能指出我正确的方向,我有错误吗?感谢
这是我使用的代码:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//disable saveButton
[_saveButton setEnabled:NO];
[self.picker removeFromSuperview];
[self.pickerToolbar removeFromSuperview];
//Initialize Data
_pickerData = @[@"Item 1", @"Item 2", @"Item 3", @"Item 4"];
//Connect Data
self.picker.dataSource = self;
self.picker.delegate = self;
_textFieldCategory.inputView = _picker;
_textFieldCategory.inputAccessoryView = _pickerToolbar;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// The number of columns of data
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
// The number of rows of data
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return _pickerData.count;
}
// The data to return for the row and component (column) that's being passed in
- (NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return _pickerData[row];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
self.textFieldCategory.text = _pickerData[row];
}
- (IBAction)doneButtonTapped:(id)sender
{
[self.picker removeFromSuperview];
[self.pickerToolbar removeFromSuperview];
答案 0 :(得分:1)
尝试创建实例变量,例如
NSInteger selectedRow;
在pickerView:didSelectRow:inComponent:
方法中,将您拥有的行替换为
selectedRow = row;
在doneButtonTapped:
方法中,添加行
self.textFieldCategory.text = _pickerData[selectedRow];