1

我是 Objective-C 的新手

我想UIDatePicker在按钮单击后在弹出窗口中显示 ..

我有一个按钮,当我单击该按钮时,我的弹出窗口应该与 DatePicker 一起出现,然后在选择日期后弹出窗口应该关闭并在文本框中设置选定的日期。

我怎样才能做到这一点 ?

为了创建一个日期选择器,我编写了这段代码..

UIDatePicker *datePicker=[[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode=UIDatePickerModeDate;
[self.view addSubview:datePicker];

但我不知道如何在按钮单击的弹出窗口中显示它..?

4

6 回答 6

1

试试这个,这可以帮助你

-(IBAction)DatePickerAction:(id)sender
{


     UIActionSheet *menu = [[UIActionSheet alloc] initWithTitle:@"Date Picker" 
                                                          delegate:self
                                                 cancelButtonTitle:@"Cancel"
                                            destructiveButtonTitle:nil
                                                 otherButtonTitles:nil];

        // Add the picker
        UIDatePicker *pickerView = [[UIDatePicker alloc] init];
        pickerView.datePickerMode = UIDatePickerModeDate;
        [menu addSubview:pickerView];
        [menu showInView:self.view];        
        [menu setBounds:CGRectMake(0,0,320, 500)];

        CGRect pickerRect = pickerView.bounds;
        pickerRect.origin.y = -100;
        pickerView.bounds = pickerRect;

        [pickerView release];
        [menu release];

    }
于 2012-10-08T12:02:35.450 回答
1

您可以将文本字段的 .inputView 属性设置为日期选择器。

所以myTextField.inputView = datePicker;

这将替换 datePicker 的键盘输入视图。

更深入一点,您需要为日期选择器的值更改时添加一个目标(因此它会更新文本字段)

让我知道这是否是您想要的方式。

如果您仍然想要弹出窗口,我不确定您是否可以为此子类化UIAlertView..

或者您可以将 datePicker 添加到一个UIView实例中,然后使用一些精美的动画等将其添加到屏幕上......

有很多选择,我很乐意为您提供帮助...

于 2012-10-08T12:02:53.040 回答
1

.h 文件中的声明

UIActionSheet *aac;  
UIDatePicker *theDatePicker; 

.m 文件中的实现

// 在注释后添加代码

-(void)DatePickerDoneClick:(id)sender {

        NSDateFormatter *df=[[[NSDateFormatter alloc]init] autorelease];
        df.dateFormat = @"MM/dd/yyyy";
        NSArray *temp=[[NSString stringWithFormat:@"%@",[df stringFromDate:theDatePicker.date]] componentsSeparatedByString:@""];

        [dateString1 release];
        dateString1=nil;
        dateString1 = [[NSString alloc]initWithString:[temp objectAtIndex:0]];
    UITextField* BirthDayTxtLBl.text = [NSString stringWithFormat:@" %@",dateString1];

NSString *theTime = [NSString stringWithFormat:@"%@",BirthDayTxtLBl.text];
        NSLog(@"%@",theTime);
        [aac dismissWithClickedButtonIndex:0 animated:YES];



        }     





- (void)DatePickercancelClick:(id)sender{

    [aac dismissWithClickedButtonIndex:0 animated:YES];
}

-(IBAction)AddTheTimePicker:(id)sendar {

    aac = [[UIActionSheet alloc] initWithTitle:[self isViewPortrait]?@"\n\n":nil  delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    theDatePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0.0, 44.0, 0.0, 0.0)];
    theDatePicker.datePickerMode=UIDatePickerModeDateAndTime;
    UIToolbar *pickerDateToolbar = [[UIToolbar alloc] initWithFrame:[self isViewPortrait]?CGRectMake(0, 0, 320, 44):CGRectMake(0, 0, 320, 44)];
    pickerDateToolbar.barStyle = UIBarStyleBlackOpaque;
    [pickerDateToolbar sizeToFit];

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

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(DatePickerDoneClick:)];
    //doneBtn.tag = tagID;
    [barItems addObject:doneBtn];


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

    UILabel *toolBarItemlabel;
    if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
        toolBarItemlabel= [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 180,30)];
    else
        toolBarItemlabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200,30)];

    [toolBarItemlabel setTextAlignment:UITextAlignmentCenter];  
    [toolBarItemlabel setTextColor:[UIColor whiteColor]];   
    [toolBarItemlabel setFont:[UIFont boldSystemFontOfSize:16]];    
    [toolBarItemlabel setBackgroundColor:[UIColor clearColor]]; 
    toolBarItemlabel.text = [NSString stringWithFormat:@"Select Start Time"];

    UIBarButtonItem *buttonLabel =[[UIBarButtonItem alloc]initWithCustomView:toolBarItemlabel];
    [toolBarItemlabel release]; 
    [barItems addObject:buttonLabel];   
    [buttonLabel release];  

    [barItems addObject:flexSpace];

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

    [pickerDateToolbar setItems:barItems animated:YES];
    [aac addSubview:pickerDateToolbar];
    [aac addSubview:theDatePicker];

    CGRect myImageRect = CGRectMake(0.0f, 300.0f, 320.0f, 175.0f);;
    [aac showFromRect:myImageRect inView:self.view animated:YES ];

    [UIView beginAnimations:nil context:nil];
    if([self interfaceOrientation] == UIInterfaceOrientationPortraitUpsideDown || [self interfaceOrientation] == UIInterfaceOrientationPortrait)
        [aac setBounds:CGRectMake(0,0,320, 464)];
    else
        [aac setBounds:CGRectMake(0,0,480, 400)];       

    [UIView commitAnimations];

}

// Add this if you wish to add support Orientation support for picker
  - (BOOL) isViewPortrait {
     UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
     return (currentOrientation == UIInterfaceOrientationPortrait || currentOrientation == UIInterfaceOrientationPortraitUpsideDown);
}
于 2012-10-08T12:15:42.147 回答
0

写在你的 Button Click 方法中

    self.DatePicker= [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];
    self.DatePicker.datePickerMode = UIDatePickerModeDate;
    [self.DatePicker addTarget:self
                   action:@selector(SetDatePickerTime:)
         forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.DatePicker];


- (void)SetDatePickerTime:(id)sender
{
     [self.DatePicker removeFromSuperview];
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"dd:MM:yyyy"];


    NSLog(@"%@",[outputFormatter stringFromDate:self.DatePicker.date]);
}
于 2012-10-08T12:22:18.393 回答
0

最好的方法是创建类似的动画..我尝试使用扩展至 uiview..

在要在正常实现上方显示弹出窗口的 .m 文件中尝试以下操作

@interface UIView (AlertAnimation)

- (void)doPopInAnimation;

@end

const NSTimeInterval kAnimationDuration = 0.3f;

@implementation UIView (AlertAnimation)

- (void) doPopInAnimation {
    CALayer *viewLayer = self.layer;
    CAKeyframeAnimation *popInAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];

    popInAnimation.duration = 0.3f;
    popInAnimation.values = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:1.2], [NSNumber numberWithFloat:0.9], [NSNumber numberWithFloat:1], nil];
    popInAnimation.keyTimes = [NSArray arrayWithObjects:[NSNumber numberWithFloat:0.0], [NSNumber numberWithFloat:0.6], [NSNumber numberWithFloat:0.8], [NSNumber numberWithFloat:1.0], nil];    

    [viewLayer addAnimation:popInAnimation forKey:@"transform.scale"];
}

@end

进一步把这条神奇的线放在你想要弹出的事件上

[yourPickerView doPopInAnimation];
于 2012-10-08T13:21:11.143 回答
0

参考这段代码

  startDatePicker = [[UIDatePicker alloc] init];
    startDatePicker.datePickerMode = UIDatePickerModeDate;
    [startDatePicker addTarget:self action:@selector(startLabelChange:)forControlEvents:UIControlEventValueChanged];
    CGSize pickerSize = [startDatePicker sizeThatFits:CGSizeZero];
    startDatePicker.frame = CGRectMake(-40, -20, pickerSize.width, pickerSize.height);
    startDatePicker.transform = CGAffineTransformMakeScale(0.750f, 0.750f);


    UIViewController *pickerController = [[UIViewController alloc] init];
    [pickerController.view addSubview:startDatePicker];
    [startDatePicker release];
    [pickerController setContentSizeForViewInPopover:CGSizeMake(240, 180)];
    UIPopoverController *pickerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
    [pickerPopover presentPopoverFromRect:startDatelabel.frame
                                   inView:rightSideView
                 permittedArrowDirections:UIPopoverArrowDirectionLeft
                                 animated:YES];
    pickerPopover.delegate = self;
    self.popover = pickerPopover;
    [pickerController release];
    [pickerPopover release];
于 2013-02-04T11:34:38.573 回答