【问题标题】:Xcode Saving UIDatepicker to be displayed in a labelXcode 保存 UIDatepicker 以显示在标签中
【发布时间】:2011-01-30 04:45:29
【问题描述】:

下面的代码是为了展示我的UIDatepicker:

- (IBAction)showActionSheet:(id)sender {
    NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
    UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                                  initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"Please Select A Date", @"")]
                                  delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"I've Made My Choice", nil];
    [actionSheet showInView:self.view];
    UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
    datePicker.datePickerMode = UIDatePickerModeDate;
    [actionSheet addSubview:datePicker];
}

一切正常,但我想做的是将用户选择显示到我命名为“label1”的标签中。任何人都可以帮忙吗?谢谢!

更新:

这是更新后的代码:

- (IBAction)showActionSheet:(id)sender {
NSString *title = UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation) ? @"\n\n\n\n\n\n\n\n\n" : @"\n\n\n\n\n\n\n\n\n\n\n\n" ;
UIActionSheet *actionSheet = [[UIActionSheet alloc] 
                              initWithTitle:[NSString stringWithFormat:@"%@%@", title, NSLocalizedString(@"Please Select A Date", @"")]
                              delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"I've Made My Choice", nil];
[actionSheet showInView:self.view];
UIDatePicker *datePicker = [[[UIDatePicker alloc] init] autorelease];
datePicker.datePickerMode = UIDatePickerModeDate;
[actionSheet addSubview:datePicker];
[datePicker addTarget:self
               action:@selector(updateLabel:)
     forControlEvents:UIControlEventValueChanged];  

}

- (void)updateLabel:(id)sender {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label1.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];
[df release];


/* the following allows you to choose the date components
 NSCalendar *calendar = [NSCalendar currentCalendar];

 int hour   =    [[calendar components:NSHourCalendarUnit    fromDate:[datePicker date]] hour];
 int minute =    [[calendar components:NSMinuteCalendarUnit  fromDate:[datePicker date]] minute];

 int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[datePicker date]] year];
 int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[datePicker date]] month];
 int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[datePicker date]] day];
 */

}
那是针对 .m 的,我的 .xib 也有正确绑定的“标签”:) 每当我选择日期时都会得到一个(空)值 :(

【问题讨论】:

    标签: iphone uidatepicker uiactionsheet


    【解决方案1】:

    .h

    #import <UIKit/UIKit.h>
    @interface YourViewController : UIViewController {
        IBOutlet UILabel *label1;
    }
    @property (nonatomic, retain) UILabel *label1;
    @end
    

    .m

    @implementation YourViewController
    @synthesize label1;
    /*
         All of your code goes here
     */
    @end
    

    界面生成器


    还将以下内容添加到您的showActionSheet:

    [datePicker addTarget:self
                   action:@selector(updateLabel:)
         forControlEvents:UIControlEventValueChanged];
    

    这应该会从datePicker更新您的标签:

    -(void)updateLabel:(id)sender
    {
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateStyle = NSDateFormatterMediumStyle;
        label1.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];
        [df release];
    
    
        /* the following allows you to choose the date components
        NSCalendar *calendar = [NSCalendar currentCalendar];
    
        int hour   =    [[calendar components:NSHourCalendarUnit    fromDate:[datePicker date]] hour];
        int minute =    [[calendar components:NSMinuteCalendarUnit  fromDate:[datePicker date]] minute];
    
        int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[datePicker date]] year];
        int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[datePicker date]] month];
        int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[datePicker date]] day];
          */
    }
    

    【讨论】:

    • 当我添加它时,我得到“datePicker”未声明,但是当我更新我的 .h 文件以具有 NSString *datePicker; ,我收到“在非结构或联合的情况下请求成员‘日期’”
    • 哦,是的,对不起,在你的标题中定义 datePicker,使用@property,然后在你的.m 中,使用@synthesize。然后在您的“showActionSheet”中,删除“UIDatePicker *”。
    • 是的,如果你使用 XIB,那么你需要在 header 中使用 @property 和 IBOutlet / 在 .m 中使用 @synthesize 并在 Interface Builder 中连接所有内容。
    • 当我将 datePicker 连接到标签时,它不会连接。它就像标签不在界面上一样,即使在标题中有 @property 和 IBOutlet 并且在 .m 中有 @synthesize 也是如此。在我的 .h 中:IBOutlet UILabel *label1; IBOutlet UIDatePicker *datePicker;两者都具有适当的 @property(nonatomic, retain)IBOutlet UILabel *label1; @property(非原子,保留)IBOutlet UIDatePicker *datePicker;和 @synthesized 他们都在 .m
    • datePicker 是 UIDatePicker,所以它自然不会连接到您的 UILabel。您将 label1 连接到 IB 中的 Label,然后将 datePicker 连接到 IB 中的 UIDatePicker
    【解决方案2】:

    这里我在UITableview 单元格中显示UIDatePicker

    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {   
        // Make a new view, or do what you want here    
        UIDatePicker *pv = [[UIDatePicker alloc] initWithFrame:CGRectMake(0,185,0,0)];    
        [self.view addSubview:pv];    
        return NO;    
    }
    

    如果您使用的是UITableView,请使用以下命令来显示UIDatePicker 而不是键盘。

    if (0 == indexPath.row) {
        cell.rightTextField.placeholder = @”Date”;
        cell.rightTextField.text = [coAccident strDateTime];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    UIDatePicker *datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 0, 185, 0)];
    [datePicker addTarget:self action:@selector(changeDateInLabel :) forControlEvents:UIControlEventValueChanged];
    cell.rightTextField.inputView = datePicker;
    } 
    
    - (IBAction)changeDateInLabel:(id)sender {
        UIDatePicker * datePicker = (UIDatePicker*)sender;
        CAccidentVO* coAccident = [m_arrVehicles objectAtIndex:1];
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateStyle = NSDateFormatterMediumStyle;   
        [coAccident setStrDateTime:[NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]]];  
        [df release];  
        [m_tvVehicleDetails reloadData];
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 2015-10-05
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-15
      相关资源
      最近更新 更多