1

私はObjective-Cの初心者です

UIDatePickerボタンをクリックした後、ポップアップウィンドウにを表示したい..

ボタンがあり、ボタンをクリックすると、ポップアップがDatePickerとともに表示され、日付を選択した後、ポップアップが閉じて、選択した日付がテキストボックスに設定されます。

これどうやってするの ?

日付ピッカーを作成するために、私はこのコードを書きました。

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

しかし、ボタンクリックでポップアップウィンドウに表示する方法がわかりません..?

4

6 に答える 6

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 に答える
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

you can set the .inputView property of the text field to a datepicker..

so myTextField.inputView = datePicker;

this will replace the keyboard input view for the datePicker.

Gets a little more indepth, you will need to add a target for when the value of the date picker changes (so it updates the text field)

let me know if this is how you want to go about it.

If you still want the popup window im not sure if you can subclass UIAlertView for this..

or you can add the datePicker to a UIView instance and add that onto the screen with some fancy animation, etc....

Plenty options and im happy to help you with them...

于 2012-10-08T12:02:53.040 に答える
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 に答える