用户在UITextField中使用后,解雇UIPicker?

时间:2013-07-11 15:48:48

标签: objective-c uitextfield uipickerview

我有一个UIPicker,只有当用户选择特定的UITextfield时才会显示。我遇到的问题是: 我添加了一个名为myPicker的工作选择器和一个名为field的文本字段。

我已经看了几个问这个问题,但他们主要是关于让选择器弹出而不是选择器,我没有找到一个给我正确答案。

我能够通过更改文本字段myPicker来显示inputView,但现在我的问题是我无法找到一种方法来添加附件toot bar,可能是“完成”按钮。

出于测试目的,我添加了一个带有操作的简单按钮:

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

1 个答案:

答案 0 :(得分:1)

你可以用动画来解雇它:

 - (IBAction)takeButton:(id)sender {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    [UIView setAnimationDelay:1.0];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];

    //you can customize this
    self.myPicker.frame = CGRectMake(-100, -100, 100, 200); 

    [UIView commitAnimations];

    [self.myPicker removeFromSuperview];
    [_field resignFirstResponder];
   }