1

我是IOS开发的新手。我正在开发一个应用程序,该应用程序需要用户填写表格以获取用户详细信息。详细信息包括我使用 UIDatePicker 的出生日期字段。我正在以编程方式添加那些 UIDatePicker 和 UIActionSheet,并希望更新标签上的日期。日期正在更新,但它们在同一标签上被覆盖。

这是代码

-(IBAction)dateButtonClicked:(id)sender {

    actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose Date"delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select",nil];                                                           
    [actionSheet showInView:self.view ];
    [actionSheet setFrame:CGRectMake(0, 117, 320, 383)];

}

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
    datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
    [datePickerView  setMinuteInterval:5];
    [datePickerView  setTag: kDatePickerTag];
    [actionSheet addSubview:datePickerView];
    datelabel = [[UILabel alloc] init];
    datelabel.frame = CGRectMake(55, 92, 300, 50);
    datelabel.backgroundColor = [UIColor clearColor];
    datelabel.textColor = [UIColor blackColor];
    datelabel.font = [UIFont fontWithName:@"Verdana-Bold" size: 12.0];
    datelabel.textAlignment = UITextAlignmentCenter;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat: @"MM/dd/YYYY h:mm a"];
    datelabel.text = [NSString stringWithFormat:@"%@",
                      [formatter stringFromDate:[NSDate date]]];
    [self.view addSubview:datelabel];
    NSArray *subviews = [actionSheet subviews];
    [[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
    [[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex != [actionSheet cancelButtonIndex]) {

        [datePickerView addTarget:self
                       action:@selector(LabelChange:)
             forControlEvents:UIControlEventValueChanged];


        // UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Date" message:msg delegate:nil cancelButtonTitle: @"Dismiss" otherButtonTitles:nil];
       // [alert show];
    }
}

- (void)LabelChange:(id)sender{
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    df.dateStyle = NSDateFormatterMediumStyle;
    datelabel.text = [NSString stringWithFormat:@""];
    datelabel.text = [NSString stringWithFormat:@"%@",
                      [df stringFromDate:datePickerView.date]];
}

请让我知道需要进行更改才能使此类问题无效。

4

2 回答 2

1

这段代码对我来说就像一个魅力!

在 .h 文件中

#import <UIKit/UIKit.h>

@interface EXPViewController : UIViewController <UIPickerViewDataSource,       UIPickerViewDelegate, UIActionSheetDelegate>{
UIActionSheet *pickerViewPopup;
UIDatePicker *pickerView;
IBOutlet UILabel *dateLabel;
}

- (IBAction)dateButtonClicked:(id)sender;

@end

在 .m 文件中:

-(IBAction)dateButtonClicked:(id)sender{
 [self showPickerView];
}

-(void)showPickerView {

pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];

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

UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];

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

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

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];

[pickerToolbar setItems:barItems animated:YES];
[barItems release];

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

-(void)doneButtonPressed:(id)sender {
//Do something here here with the value selected using [pickerView date] to get that value
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd, MMM YYYY"];
    NSString* dateString = [dateFormat stringFromDate:((UIDatePicker*)pickerView).date];
   yourLabel.text = dateString;

[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}

-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
于 2012-12-15T09:48:15.343 回答
0

这是因为每当您的dateButtonClicked操作触发时,datePickerView并且datelabel一次又一次地分配,所以只需做一件事,将您的代码datePickerViewdatelabel代码放在 dateButtonClicked clicked 之外,您可以放入viewDIdLoad

您可以实现选择器的委托方法,

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{

}
于 2012-12-15T09:48:24.030 回答