8

I am creating Alarm app. I want to display UIDatePicker in UIActionSheet when tapping on UITextField.

HpTerm
  • 8,151
  • 12
  • 51
  • 67
  • Have you tried something so far? Usually on StackOverflow, you should ask questions when you have tried something that doesn't work, not ask us to do what you have to do ;) Also, a google search give the answer (also on StackOverflow btw). – Zoleas Aug 23 '12 at 16:29
  • i m searching on google bt not get perfectly ans :( okay thanks :) –  Aug 23 '12 at 16:46

3 Answers3

8

For doing this you need to implement UIActionShetDelegate and UITextFieldDelegate In the textFieldDidBeginEditing you need to show the action sheet like:

-(void)textFieldDidBeginEditing:(UITextField *)sender
{
  [self showAction];
}
-(void) showAction
{
  UIActionSheet *asheet = [[UIActionSheet alloc] initWithTitle:@"Pick the date." delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select", nil];
  [asheet showInView:[self.view superview]];
  [asheet setFrame:CGRectMake(0, 117, 320, 383)];
  [asheet release];
}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
  {

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];

   //Configure picker...
   [pickerView setMinuteInterval:5];
   [pickerView setTag: kDatePickerTag];

  //Add picker to action sheet
  [actionSheet addSubview:pickerView];

  [pickerView release];

  //Gets an array af all of the subviews of our actionSheet
  NSArray *subviews = [actionSheet subviews];

  [[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
  [[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];

}
Midhun MP
  • 103,496
  • 31
  • 153
  • 200
5

I found that when using textfields with pickers its easiest to just setup the textfield's input view and add your own toolbar for convenience. Make sure your have your "UITextFieldDelegate" and picker methods setup then hook it up like so.

 yourtextfield.delegate = self;
 [yourtextfield setInputView:yourpickerview];
 [yourtextfield setInputAccessoryView:doneBar];

 doneBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
 [doneBar setBarStyle:UIBarStyleBlackTranslucent];
 UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] 
                initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                target:nil
                action:nil];
 [doneBar setItems: [NSArray arrayWithObjects:spacer2, [[UIBarButtonItem alloc] 
                    initWithTitle:@"Done"
                    style:UIBarButtonItemStyleDone
                    target:yourtextfield 
                    action:@selector(resignFirstResponder)],nil ] animated:YES];

But if you would prefer to use an action sheet then this is the basic setup:

Add UIPickerView & a Button in Action sheet - How?

Community
  • 1
  • 1
Chris Mitchelmore
  • 5,976
  • 3
  • 25
  • 33
2

You can set UIDatePicker as UITextField input view (@property inputView). ActionSheet can contain buttons only.

Midhun MP
  • 103,496
  • 31
  • 153
  • 200
Valerii Pavlov
  • 1,986
  • 1
  • 19
  • 30