0

I have this code. UIPickerView for Textfield.
I would like to desigh so that
when user edit UITextField,UIPickerView and donebutton are displayed,
UIPickerView is closed by pushing donebutton.

Problem is doneButton is not displayed.
So,Picker cannot be closed.

ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController <UITextFieldDelegate,UIPickerViewDelegate,UIPickerViewDataSource>

@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
{
    UIPickerView *picker1;
    NSString *pic1_str;
}
@synthesize textField1;
@synthesize textField2;

- (void)viewDidLoad
{
    [super viewDidLoad];

    textField1.delegate = self;

    picker1 = [[UIPickerView alloc] init];
    picker1.frame = CGRectMake(0, 460, 320, 216);
    picker1.showsSelectionIndicator = YES;
    picker1.delegate = self;
    picker1.dataSource = self;
    picker1.tag = 1;
    [self.view addSubview:picker1];
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
        [self showPicker1];
        return NO;
}

- (void)showPicker1 {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    picker1.frame = CGRectMake(0, 204, 320, 216);
    [UIView commitAnimations];

    if (!self.navigationItem.rightBarButtonItem) {
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
        [self.navigationItem setRightBarButtonItem:doneButton animated:YES];
    }
}

- (void)done:(id)sender {
    [self hidePicker];
    [self.navigationItem setRightBarButtonItem:nil animated:YES];
}

- (void)hidePicker {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2];
    [UIView setAnimationDelegate:self];
    picker1.frame = CGRectMake(0, 420, 320, 216);
    [UIView commitAnimations];
}

Any idea on how I could fix it?

Code cracker
  • 3,105
  • 6
  • 37
  • 67
Teruya Kusumoto
  • 40
  • 1
  • 2
  • 7

2 Answers2

2

You can also do that by setting the inputView of the UITextField object:

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField
{
    textField.inputView = _pickerView;
    textField.inputAccessoryView = self.accessoryView_;

    return YES;
}

where the property accessoryView_ could be instantiated with:

self.accessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, _pickerView.frame.size.width, 40)];

        [(UIToolbar *) self.accessoryView_ setItems:@[
                                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                                [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissPicker)]
         ]];

somewhere in your code (viewDidLoad would be a good place to do it)

dismissPicker is simply

- (void) dismissPicker
{
    [_textField resignFirstResponder];
}

Of course you have to keep a reference to the textField with a property or an ivar

Vik
  • 1,897
  • 12
  • 18
0

If I understand it correctly you have the problem to show the "Done" button on navigation bar (right side). The navigation item would not be able to work properly if the current UIViewController is not part of the UINavigationController hierarchy or not initialised with UINavigationController as rootViewController.

You can confirm this via the below code snippet, if it works then view controller is part of the UINavigationController, if not then you should have to check your UINavigation hierarchy.

self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"done", @"Done")
                                             style:UIBarButtonItemStyleDone
                                            target:self action:@selector(doneAction)];
Deminem
  • 672
  • 8
  • 19