解雇UIPickerView

时间:2013-10-18 18:31:45

标签: ios uipickerview

我有一个自定义的UITableView Cell

@interface PickTypeCell : UITableViewCell  <UIPickerViewDataSource, UIPickerViewDelegate>

@property (weak, nonatomic) IBOutlet UITextField *txtTextField;
@property (readwrite, retain) IBOutlet UIPickerView* dtpPicker;

@end

实施文件看起来像

@implementation PickTypeCell

@synthesize txtTextField;
@synthesize dtpPicker;

- (void)awakeFromNib
{
    NSLog(@"inside PickTypeCell init");

    //assign this to the picker delegate / datasource
    dtpPicker = [[UIPickerView alloc] init];
    dtpPicker.dataSource = self;
    dtpPicker.delegate = self;

    // assign the picker to the text field input view,so the picker is displayed when the text field receives input event
    txtTextField.inputView=dtpPicker;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    //retuns the number of columns of the picker view : 1
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    //return the number of rows for each component : the number of tree types
    return 5;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    //called to get the text to be displayed on each row of the picker
    return @"HAHAH";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //called when the user clicks on a row of the picker view
    txtTextField.text=@"HABOOOOUN";
}

加载单元格后,将显示输入文本字段。 单击文本字段时,将按预期显示包含“HAHAH”的5行选择器视图。 单击一行时,文本字段中的文本将使用“HABOOOOUN”字符串进行更新。

但是选择器仍然显示,我不知道如何解雇它。

当用户点击选择器中的任何一行时,我希望取消选择器。 如果用户再次单击文本字段,则应再次显示选择器。

我尝试在“didSelectRow”方法中使用[dtpPicker resignFirstResponder],但没有任何成功。为什么呢?

2 个答案:

答案 0 :(得分:4)

您应该尝试辞职UItextField [txtTextField resignFirstResponder]

答案 1 :(得分:0)

PickTypeCell *cell = (id)[tableView cellForRowAtIndexPath:indexPath];
[cell.txtTextField resignFirstResponder];

如果您点击textField或发送消息[textField becomeFirstResponder] textField成为第一响应者并且键盘自动出现。如果您想要隐藏键盘,请拨打[textField resignFirstResponder];您可以将键盘更改为其他视图textField.inputView = myView;已完成的操作