1
我有一個UIPicker只想在用戶選擇特定的UITextfield時顯示。我遇到的問題是這樣的: 我添加了一個名爲myPicker
的工作選取器和一個名爲field
的文本字段。用戶在UITextField中完成使用後關閉UIPicker?
我已經看過幾個問題,但他們主要是關於讓picker彈出而不是picker,我沒有遇到過與我正確的答案。
我能夠通過更改文本框的inputView
來顯示myPicker
,但現在我的問題是我無法找到一種方法來添加附加的按鈕或許「完成」按鈕。
出於測試目的,我添加了動作一個簡單的按鈕:
self.myPicker.hidden = YES;
[_field resignFirstResponder];
但是,這是給我一個奇怪的,非動畫的外觀,只是感覺不對。
這裏是我的這段代碼:
#import "ViewController.h"
@interface MixerHPViewController()
@property (weak, nonatomic) IBOutlet UILabel *testLabel;
@property (weak, nonatomic) IBOutlet UITextField *field;
@property (weak, nonatomic) IBOutlet UIPickerView *myPicker;
@property (weak, nonatomic) IBOutlet UITextField *field2;
- (IBAction)takeButton:(id)sender;
@end
@implementation ViewController
@synthesize tempList;
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return [tempList count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [tempList objectAtIndex:row];
}
- (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
CGFloat chosenValue;
switch (row) {
case 0:
chosenValue = 0.0000765;
break;
case 1:
chosenValue = 0.0000123;
break;
case 2:
chosenValue = 0.0000982;
break;
case 3:
chosenValue = 0.0000933;
break;
case 4:
chosenValue = 0.0000058;
break;
case 5:
chosenValue = 0.0000121;
break;
case 6:
chosenValue = 0.0000132;
break;
default:
chosenValue = 0;
break;
}
NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
formatter.numberStyle = NSNumberFormatterDecimalStyle;
formatter.minimumFractionDigits=7;
self.testLabel.text = [formatter stringFromNumber:@(chosenValue)];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
tempList = [[NSArray alloc] initWithObjects:@"OPTION A",@"OPTION B",@"OPTION C",@"OPTION D",@"OPTION F",@"OPTION G",@"OPTION H", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)textFieldDidBeginEditing:(UITextField *)textField {
if(textField == self.field) {
self.field.inputView = _myPicker;
}
}
- (IBAction)takeButton:(id)sender {
self.myPicker.hidden = YES;
[_field resignFirstResponder];
}
@end