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

Discussion in 'iOS Programming' started by zizos, Apr 2, 2013.

  1. zizos, Apr 2, 2013
    Last edited: Apr 4, 2013

    zizos macrumors newbie

    Joined:
    Feb 20, 2012
    #1
    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];   
    
      } 
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
  3. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #3
    Maybe take out all the #pragma marks before posting. They make your code hard to read, especially mid block.
     
  4. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #4
    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 .:)
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
  6. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
  7. dejo, Apr 4, 2013
    Last edited: Apr 4, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #7
    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.
     
  8. zizos, Apr 4, 2013
    Last edited by a moderator: Apr 4, 2013

    zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #8
     
  9. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #9
    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.
     
  10. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #10
    yes only the first textfield and when i click on others its not working :confused:
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    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?...
     
  12. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #12
    humm , maybe you are right , but can you show me how can i make this loop ??? :)
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    You already have a loop.
     
  14. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #14
    yes i know but i dont know how to use it to solve my problem:D this is why i am asking :)
     
  15. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #15
    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 .
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    Once again, you are only assigning the toolbar once, not seven times, like you should be.

    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.
     
  17. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #17
    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
     
  18. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #18
    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.
     
  19. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #19
    i am getting this error

    use of undeclared identifier "toolBar" :confused:
     
  20. dejo, Apr 5, 2013
    Last edited: Apr 6, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #20
    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.
     
  21. zizos thread starter macrumors newbie

    Joined:
    Feb 20, 2012
    #21
    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 .
     
  22. dejo, Apr 8, 2013
    Last edited: Apr 8, 2013

    dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #22
    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.
     

Share This Page