put UIDatePicker value into TextField?

Discussion in 'iOS Programming' started by Brendan.Porter, Jul 31, 2008.

  1. macrumors member

    Joined:
    May 19, 2007
    Location:
    Between tourists, corn, and windmills.
    #1
    I'm trying to simply place the TIME from a UIDatePicker into a UITextField.
    I am beating my head against the wall looking at the code for the last week.

    The code I have is currently:
    -(void) createDatePicker
    {
    [myDatePicker = [UIDatePicker alloc] awakeFromNib];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    NSString* currentTime = [dateFormatter stringFromDate:[NSDate date]];
    [dateFormatter release];

    }

    In my head the 7th line's "currentTime" should hold the date now in a string. However, in another method when I try to set the text of the UITextField to currentTime, nothing happens.

    - (IBAction) buttonPress: calculateButton
    {
    [timeInField setText:currentTime];
    }

    Any help?
     
  2. macrumors member

    Joined:
    Mar 31, 2008
    #2
    currentTime is delcared inside the function 'createDatePicker' so its scope is limited to that function.
     
  3. thread starter macrumors member

    Joined:
    May 19, 2007
    Location:
    Between tourists, corn, and windmills.
    #3
    Ok, I get that.
    I have a declaration of currentTime as an NSString in the header, so that isn't necessary.

    I've changed the code a bit, and now do not receive warnings.
    However, the text from theTime still does not put itself into the UITextField.

    I've even prototyped the plumbing by placing a test string within the function that is supposed to put the date into UITextField, and that string does show in the field properly when the button is pressed. This tells me that I'm missing a critical piece of plumbing to either get the value of [myDatePicker date] into theTime, or theTime looses the value outside of the createDatePicker function.
    I even tried a [theTime retain], but that didn't help.

    -(void) createDatePicker
    {
    [myDatePicker = [UIDatePicker alloc] awakeFromNib];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterNoStyle];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    theTime = [dateFormatter stringFromDate:[myDatePicker date]];
    [dateFormatter release];

    }


    - (IBAction) buttonPress: calculateButton
    {
    NSString* tryThisString = @"Test";
    [timeInField setText:[tryThisString description]];
    }



    Any further help would be greatly appreciated.
     
  4. macrumors member

    Joined:
    Mar 31, 2008
    #4
    it may be that you didn't link it to that function properly inside the Interface Builder.
     

Share This Page