Search code examples
xcodehideuitoolbar

how can i show only picker and hid the keyboard ???


I'm trying to show only the picker and hide the keyboard but it's working only with the first textfield.

I got a textfield that I made and when I click on the first textfield the picker popups but when I click on the other textfield the keyboard popups.

How can I only show the picker?

------------ MY CODE --------

ViewController.h

interface ViewController : UIViewController <UITextFieldDelegate >{

IBOutlet UIDatePicker *myDatePicker;
UITextField* myTextField;

int x ; int y ; int w ; int h ;  int moretext ;   }   @end

ViewController.m

- (void)viewDidLoad {

 myTextField.delegate = self;    

[super viewDidLoad];

pragma mark - myTextField

x = 36;  y = 50;   w = 36;   h = 25 ;  moretext = 0 ;

for (moretext=0; moretext<7; moretext ++) {

    myTextField = [[UITextField alloc]initWithFrame:CGRectMake(x, y, w, h)];
    myTextField.textAlignment = NSTextAlignmentCenter;
    myTextField.backgroundColor = [UIColor grayColor];
    myTextField.font = [UIFont fontWithName:@"Helvetica" size:(17)];
    myTextField.layer.borderWidth = 0.70 ;
    myTextField.layer.borderColor =[ UIColor orangeColor].CGColor ;
    myTextField.returnKeyType = UIReturnKeyDone;

          x+=36 ;

    [self.view addSubview:myTextField];

}

pragma mark - myDatePicker

myDatePicker.date = [NSDate date];

[myDatePicker addTarget:self action:@selector(pickerChanged:)    forControlEvents:UIControlEventValueChanged];

myTextField.inputView = myDatePicker;   //link text filed with picker

pragma mark - UIToolbar

UIToolbar* toolBar = [[UIToolbar alloc] init];
toolBar.barStyle = UIBarStyleBlack;
toolBar.translucent = YES;
toolBar.tintColor = nil;
[toolBar sizeToFit];

UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc] initWithTitle:@"Done"
                                                                style:UIBarButtonItemStyleBordered target:self
                                                               action:@selector(pickerHide:)] autorelease];

[toolBar setItems:[NSArray arrayWithObjects:doneButton, nil]];

myTextField.inputAccessoryView = toolBar; 

[toolBar release];    }

pragma mark - datePickerChanged

 - (void)pickerChanged:(id)sender{

 NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

 [formatter setDateFormat:@"h:mm"];

 NSString* myDate = [formatter stringFromDate:myDatePicker.date];

 NSLog(@"date is: %@", myDate);

 myTextField.text = myDate;

 [formatter release];     }

Solution

  • i found that all what i need is to put myDatePicker and my toolBar in the loop and thats all . hop my answer to my question help others .