1

I have a UIPicker that would like to be shown only when a user selects a specific UITextfield. The problem I am having is this: I have added a working picker called myPicker and a text field called field.

I have looked at several questions asking this but they are mostly about getting the picker to pop up instead of the picker, I have not come across with one with the right answer for me.

I was able to get myPicker to show up by changing the text field's inputView but now my problem is that I cannot figure out a way to add an accessory toot bar with perhaps a "Done" button.

For testing purposes I added a simple button with the action :

self.myPicker.hidden = YES;

[_field resignFirstResponder];

But this is giving me a weird, non animated look that just doesn't feel right.

Here is my code for this:

#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
vzm
  • 2,440
  • 6
  • 28
  • 47

1 Answers1

1

You can dismiss it with animations:

 - (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];
   }
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69