我有一个 UIPicker,它希望仅在用户选择特定 UITextfield 时显示。我遇到的问题是:我添加了一个名为的工作选择器myPicker
和一个名为field
.
我看过几个问这个的问题,但它们主要是关于让选择器弹出而不是选择器,我还没有遇到一个对我来说正确答案的问题。
我可以myPicker
通过更改文本字段来显示,inputView
但现在我的问题是我无法找到一种方法来添加可能带有“完成”按钮的附件嘟嘟栏。
出于测试目的,我添加了一个带有操作的简单按钮:
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