iphone - Xcode 保存要在标签中显示的 UIDatepicker

标签 iphone uidatepicker uiactionsheet

以下代码用于显示我的 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 也正确绑定(bind)了“标签”:) 每当我选择日期时就会得到一个(空)值:(

最佳答案

.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

界面生成器

enter image description here

<小时/>

还将以下内容添加到您的 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];
      */
}

关于iphone - Xcode 保存要在标签中显示的 UIDatepicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4841205/

相关文章:

ios - 如何为 iOS 应用程序测试设置仅 IPv6 网络?

iphone - 单独使用 UINavigationViewController

c# - 为什么我的 UIPickerView 在选择时崩溃

ios - 适用于 iOS 的自定义 UIActionSheet

iphone - UIActionSheet 按钮颜色问题

iphone - 寻找 : iOS 6 NSAttributedString Descenders Block Underline 的解决方法

iphone - UITableView 将无法正确更新(即使调用 reloadData)

ios - subview 位置不正确

ios - UIDatePicker 最小日期和最大日期

ios - iOS 中没有分钟和秒的 UIDateTime Picker