0

I try to add a date picker in action sheet , also give another button

It looks like the image

enter image description here

I can get the time when I roll the picker

But that small Done button not work...

I can't even to touch it.

This is my code ,not too much ...

I can't figure why the button not working ?

- (IBAction)selectDate:(id)sender{
menu = [[UIActionSheet alloc] initWithTitle:@"Select Date"
                                                  delegate:self
                                         cancelButtonTitle:nil
                                    destructiveButtonTitle:nil
                                         otherButtonTitles:nil];
[menu setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

startPickerView = [[UIDatePicker alloc] init];
startPickerView.datePickerMode = UIDatePickerModeTime;
[menu sendSubviewToBack:startPickerView];
[menu showInView:self.view];        
[menu setBounds:CGRectMake(0,0,320,460)];
CGRect pickerRect = startPickerView.bounds;
pickerRect.origin.y = -60;
startPickerView.bounds = pickerRect;
[startPickerView addTarget:self action:@selector(timeChange:) forControlEvents:UIControlEventValueChanged];

UISegmentedControl *closeButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:@"Done"]];
closeButton.momentary = YES; 
closeButton.frame = CGRectMake(260, 7.0f, 50.0f, 30.0f);
closeButton.segmentedControlStyle = UISegmentedControlStyleBar;
closeButton.tintColor = [UIColor blackColor];
[closeButton addTarget:self action:@selector(dismissActionSheet:) forControlEvents:UIControlEventTouchUpInside];

[menu addSubview:startPickerView];

[menu addSubview:closeButton];
[closeButton release];  
[menu release];
[startPickerView release];

}

Many thanks for any reply~

Webber

Webber Lai
  • 2,014
  • 5
  • 35
  • 66

2 Answers2

3

I've did something similar to this before using the below code. Here I create a date pickerview and add to an action sheet with two buttons: Done and Cancel in a toolbar.

- (IBAction)selectDate:(id)sender{   
UIActionSheet *pickerViewPopup = [[UIActionSheet alloc] init];

   const CGFloat toolbarHeight = 44.0f;

    UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, toolbarHeight, 0, 0)];
    pickerView.datePickerMode = UIDatePickerModeDate;
    pickerView.hidden = NO;
    pickerView.date = [NSDate date];

    UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, toolbarHeight)];
    pickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
    [barItems addObject:doneBtn];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
    [barItems addObject:cancelBtn];

    [pickerToolbar setItems:barItems animated:YES];

    [pickerViewPopup addSubview:pickerToolbar];
    [pickerViewPopup addSubview:pickerView];
    [pickerViewPopup showInView:self.view];
    [pickerViewPopup setBounds:CGRectMake(0,0,self.view.frame.size.width, 464)];
}

An example of where this is used can be found at: http://www.buggyprogrammer.co.uk/2010/08/18/open-uipickerview-when-user-enters-textfield/

wibosco
  • 967
  • 1
  • 11
  • 32
  • I try to follow the this http://stackoverflow.com/questions/1262574/add-uipickerview-a-button-in-action-sheet-how I will try you answer now ,Thanks !!! – Webber Lai Jun 28 '11 at 08:14
  • @webberLai that looks very similar solution, hopefully one of them will work for you – wibosco Jun 28 '11 at 08:16
  • your code works , But can you tell me not use the tool bar ,and with the cancel and done buttons ??? – Webber Lai Jun 28 '11 at 08:19
  • @webberLai I've not tried it without the toolbar (and am at work just now so can't create a new project to experiment) but to remove any of the button you just delete their declaration or don't add them to the "barItems" array – wibosco Jun 28 '11 at 08:32
0

I think the way you use the UIActionSheet is the source for your problems : a UIActionSheet listens for events on the buttons on the sheet .. and the way you are using it you overlay it with your own components.

I would personally create a custom UIView and show that instead of putting the DatePicker and the Done button on top of a UIActionSheet.

werner
  • 859
  • 4
  • 8
  • But even I give it a cancel button as default button still not work ,can you tell me why ??Is my picker view cover the button layer ? – Webber Lai Jun 28 '11 at 07:53