Hey tôi đã dán trong một số mã tôi đã viết một thời gian trở lại để trang trải kịch bản này. Có hai ví dụ một với một actionsheet và một không có một actionsheet:
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
CGRect pickerFrame = CGRectMake(0, 40, 0, 0);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.dataSource = self;
pickerView.delegate = self;
[actionSheet addSubview:pickerView];
[pickerView release]; //NB this may result on the pickerview going black the next time you come back to the textfield, if this is the case just remove this statement
UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:NSLocalizedString(@"SUBMIT", @"")]];
closeButton.momentary = YES;
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventValueChanged];
[actionSheet addSubview:closeButton];
[closeButton release];
[actionSheet showInView:displayedInView];
[actionSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
Đây là mã mà không actionsheet:
- (void)textFieldDidBeginEditing:(UITextField *)myTextField{
[myTextField resignFirstResponder];
for (int component = 0; component < (((NSInteger)numberOfComponentsForPickerView) - 1); component++) {
NSInteger valueForRow = [[self.textField.text substringWithRange:NSMakeRange(component,1)] integerValue];
[pickerView selectRow:valueForRow inComponent:component animated:YES];
}
[self fadeInPickerView];
[view addSubview:pickerView];
[pickerView release];
}
Một ví dụ chi tiết hơn về điều này có thể được tìm thấy tại địa chỉ: http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/
giải pháp của bạn hoạt động một phần. Tôi muốn hiển thị xem bộ chọn trên một số uitextfields cụ thể không phải trên tất cả các trường văn bản và cũng khi giá trị được chọn từ giao diện bộ chọn được điền vào trường văn bản mong muốn, chế độ xem bộ chọn phải được ẩn. –