Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zizos

macrumors newbie
Original poster
Feb 20, 2012
12
0
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 --------:D

ViewController.h

Code:
 interface ViewController : UIViewController <UITextFieldDelegate >{

IBOutlet UIDatePicker *myDatePicker;

UITextField* myTextField;

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

ViewController.m

Code:
- (void)viewDidLoad {

 myTextField.delegate = self;    

[super viewDidLoad];

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];

}


myDatePicker.date = [NSDate date];

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

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


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];    

}

- (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];   

  }
 
Last edited:
Maybe take out all the #pragma marks before posting. They make your code hard to read, especially mid block.
 
you are right It is considered impolite to cross-post .

but i dont know how to post my codes here as i did in StackOverflow community , and i hop to find an answer for my question in StackOverflow community , or here .:)
 
Um, close, but no cigar.
Code:
 tags, not QUOTE tags. Also, please show the code as two blocks, one for the .h and one for the .m, rather than broken up by pragma sections. Thank you.[/QUOTE]

thanks again for your help :D
 
Last edited by a moderator:
Okay, here's where I think your problem lies:
Code:
myTextField.inputView = myDatePicker;

You have 7 textFields, correct? But this line is only called once.
 
yes only the first textfield and when i click on others its not working :confused:
 
So, you need to set the inputView for each seven of your myTextFields. Hmm, now where do you have a loop that goes through all seven textFields?...
 
So, you need to set the inputView for each seven of your myTextFields. Hmm, now where do you have a loop that goes through all seven textFields?...

humm , maybe you are right , but can you show me how can i make this loop ??? :)
 
by the way if you trying to say that i need to put in the loop

Code:
    myTextField.inputView = myDatePicker;

i know this will work but it wil give me more problems :(

1- the toolBar will not work with other text field
2- the picker value work only with the first textfield every time i change the picker value in any textfield .
 
1- the toolBar will not work with other text field
Once again, you are only assigning the toolbar once, not seven times, like you should be.

2- the picker value work only with the first textfield every time i change the picker value in any textfield .
Sorry, I'm not exactly sure what you're saying here, but I suspect the issue might be: you have seven textFields but myTextField is only able to reference one of them, and you continue to rely upon that single reference throughout the code. You need to figure out a way to keep track of which textField is triggering your methods so that you can access its properties appropriately.
 
if you mean that i have to put in my loop

Code:
 myTextField.inputAccessoryView = toolBar;


to show the toolbar with all textfield i am sorry to tell you that its not working
 
if you mean that i have to put in my loop

Code:
 myTextField.inputAccessoryView = toolBar;


to show the toolbar with all textfield i am sorry to tell you that its not working

Not working how? Are you getting warnings, errors, crashes, what? Have you done any debugging? If so, what? Please provide enough information for someone else to be able to know what your problem is.
 
i am getting this error

use of undeclared identifier "toolBar" :confused:
 
You're using a variable before you declare it. This is basic programming that you're violating. I'm gonna suggest you step away from the real coding and spend some time learning the fundamentals of Objective-C programming before you come back to this issue.
 
Last edited:
You're using a variable before you declare it. This is basic programming that you're violating. I'm gonna suggest you step away from the real coding and spend some time learning the fundamentals of Objective-C programming before you come back to this issue.

thanks for the advice and know , even after declaring it , it's not working and i am not getting any warnings, errors, crashes, :confused: its just not working if its like i did nothing .
 
Go get comfortable with the fundamentals. Revisit this issue when you are. By then, you'll hopefully be able to debug what you're doing wrong. Good luck.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.