开发者

Help With UIDatePicker In UIActionSheet

I used some modified code found from another question on this website.

I am not sure however exactly what theDatePicker and dtpicker are. I need to declare them but I don't know exactly if they are supposed to be a UIDatePicker and why there are two of them. Can anyon开发者_如何学Ce figure it out? Thanks.

- (IBAction)dateButtonPressed1
{
    UIActionSheet *aac = [[UIActionSheet alloc] initWithTitle:@"Select Date"
                                                     delegate:self
                                            cancelButtonTitle:nil
                                       destructiveButtonTitle:nil
                                            otherButtonTitles:nil];
    self.dtpicker = theDatePicker;
    [theDatePicker release];
    [dtpicker addTarget:self action:@selector(dateChanged) forControlEvents:UIControlEventValueChanged];

    pickerDateToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerDateToolbar 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(DatePickerDoneClick)];
    [barItems addObject:doneBtn];

    [pickerDateToolbar setItems:barItems animated:YES];

    [aac addSubview:pickerDateToolbar];
    [aac addSubview:dtpicker];
    [aac showInView:self.view];
    [aac setBounds:CGRectMake(0,0,320, 464)];
}

Link to original question where I found code: Add UIPickerView & a Button in Action sheet - How?


UIActionSheet *dateActionSheet = [[UIActionSheet alloc] initWithTitle:@""
                                                                     delegate:self
                                                            cancelButtonTitle:@"Cancel"
                                                       destructiveButtonTitle:nil
                                                            otherButtonTitles:@"Done", nil];
        [dateActionSheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];
        [dateActionSheet showInView:self.view];
        [dateActionSheet setFrame:CGRectMake(0, 200, 320, 383)];
        [dateActionSheet release];

Then after you have to write in the ActionSheet Delegate's this below method:

#define kPickerTag 200
#define SelectButtonIndex 1
#define CancelButtonIndex 2
-(void)willPresentActionSheet:(UIActionSheet *)actionSheet {

UIDatePicker *pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, 100, 116)];
    [pickerView setTag:kPickerTag];

    [pickerView setDatePickerMode:UIDatePickerModeDate];
        if(!(strPrintDate == @""))
        {
            NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
            [dateFormat setDateFormat:@"MMM-dd-yyyy"];
            NSDate *pickerdate = [dateFormat dateFromString:self.strPrintDate];
            [pickerView setDate:pickerdate animated:YES];
            [dateFormat release];
        }
    [actionSheet addSubview:pickerView];

    [pickerView release];

    NSArray *subViews = [actionSheet subviews];

    [[subViews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(0, 5, 75, 46)];
    [[subViews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(225, 5, 85, 46)];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜