简体   繁体   English

退出UIPickerView

[英]Dismissing UIPickerView

I have a custom UITableView Cell 我有一个自定义的UITableView单元格

@interface PickTypeCell : UITableViewCell  <UIPickerViewDataSource, UIPickerViewDelegate>

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

@end

The implementation file looks like 实现文件看起来像

@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";
}

When the cell is loaded, the input text field is displayed. 加载单元格后,将显示输入文本字段。 When clicking on the text field, the picker view is displayed with 5 lines containing "HAHAH", as expected. 单击文本字段时,按预期显示选择器视图,其中包含5行,其中包含“ HAHAH”。 When clicking a line, the text in the text field is updated with the "HABOOOOUN" string. 单击一行时,将使用“ HABOOOOUN”字符串更新文本字段中的文本。

But the picker is still displayed and I don't know how to dismiss it. 但是选择器仍然显示,我不知道如何将其关闭。

I'd like the picker to be dismissed when the user clicks any line in the picker. 我希望在用户单击选择器中的任何行时将其关闭。 If the user clicks on the text field again, the picker shall be displayed again. 如果用户再次单击文本字段,则应该再次显示选择器。

I tried using [dtpPicker resignFirstResponder] in the "didSelectRow" method, without any success. 我尝试在“ didSelectRow”方法中使用[dtpPicker resignFirstResponder],但没有成功。 Why? 为什么?

您应该尝试退出UItextField [txtTextField resignFirstResponder]

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

If you tap on textField or send message [textField becomeFirstResponder] textField became first responder and keyboard automatically appear. 如果您单击textField或发送消息[textField becomeFirstResponder] textField成为[textField becomeFirstResponder] textField成为第一响应者,并且键盘会自动出现。 If you want to hide keyboard you should call [textField resignFirstResponder]; 如果要隐藏键盘,则应调用[textField resignFirstResponder]; You can change keyboard to other view textField.inputView = myView; 您可以将键盘更改为其他视图textField.inputView = myView; what you already done 你已经做了什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM