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?