Vaibhav Jhaveri
Vaibhav Jhaveri

Reputation: 1605

Unable to fetch date from UIDatePicker which is used inside of UIActionSheet which opens on TextField click

My Method where the acton sheet is called

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [txtSourceTime resignFirstResponder];

    UIActionSheet *sheet1 = [[UIActionSheet alloc] initWithTitle:@"Select Source time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    [sheet1 setActionSheetStyle:UIActionSheetStyleDefault];
    [sheet1 setTag:3];
    [sheet1 showInView:self.view];

    time = [[UIDatePicker alloc] init];
    time.datePickerMode = UIDatePickerModeDateAndTime;
    time.timeZone = [NSTimeZone localTimeZone];

    [time setBounds:CGRectMake(0, -125, 320, 200)];
    [sheet1 addSubview:time];
    [sheet1 showFromRect:CGRectMake(0, 300, 320, 300) inView:self.view animated:YES];
    [sheet1 setBounds:CGRectMake(0, 0, 320, 500)];
}

Unable to get the date in the textbox when i click done in actionsheet

Upvotes: 0

Views: 108

Answers (2)

bobnoble
bobnoble

Reputation: 5824

Need to add an UIActionSheetDelegate method to know when the Done button is tapped and then update the UITextField with the date from the UIDatePicker. E.g.:

In the .h:

Add the UIActionSheetDelegate protocol:

@interface MyClass : UIViewController <UIActionSheetDelegate, UITextFieldDelegate>
    // ...
@end

In the .m:

Add UIActionSheet delegate method to know when action sheet is being dismissed (i.e., one of the buttons is tapped):

-(void)actionSheet:(UIActionSheet *)actionSheet willDismissWithButtonIndex:(NSInteger)buttonIndex{

    // buttonIndex of 1 is the Done button
    if (buttonIndex == 1) {
        NSDate *date = time.date;

        // Date shows in text field, but is not nicely formatted
        textField.text = date.description;
    }
}

and set the delegate for the UIActionSheet:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    //...

    UIActionSheet *sheet1 = [[UIActionSheet alloc] initWithTitle:@"Select Source time" delegate:self cancelButtonTitle:nil destructiveButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil];
    sheet1.delegate = self;

    //...
}

Do note that per Apple's documentation, this is not the recommended use of an UIActionSheet:

UIActionSheet is not designed to be subclassed, nor should you add views to its hierarchy.

Another alternative is to add the UIDatePicker as the inputView on the UITextField. For the Cancel and Done buttons, add an inputAccessoryView on the UITextField (which would show at the top of the UIDatePicker.

Upvotes: 1

ssmanohar
ssmanohar

Reputation: 186

Add below code.....

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        printf("\n cancel");

    }else{
      printf("\n Done");
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm"];
        NSString *strDate = [dateFormatter stringFromDate:time.date];
        _txtSourceTime.text = strDate;

    }
}

Upvotes: 0

Related Questions