ios 如何从目标c中的日期选择器获取日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14700725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:09:01  来源:igfitidea点击:

How to get date from datepicker in objective c?

iosobjective-cxcode

提问by sharataka

In header file I have:

在头文件中,我有:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

- (IBAction)displayDay:(id)sender;
@property (weak, nonatomic) IBOutlet UIDatePicker *datePicker;

@end

Implementation:

执行:

- (IBAction)displayDay:(id)sender {

    NSDate *chosen = [UIDatePicker date];

}

Xcode is telling me I should have UIdatePicker, but when I make that change it says "No know class method for selector 'date'".

Xcode 告诉我应该有 UIdatePicker,但是当我进行更改时,它说“不知道选择器‘日期’的类方法”。

Any advice on how to deal with this?

有关如何处理此问题的任何建议?

回答by Midhun MP

The following code won't work:

以下代码将不起作用:

NSDate *chosen = [UIDatePicker date];

Because the dateis not a class method it's a property of UIDatePickerclass.

因为date它不是类方法,所以它是UIDatePicker类的属性。

date

The date displayed by the date picker.

@property(nonatomic, retain) NSDate *date;

Discussion

The default is the date when the UIDatePicker object is created. The date is ignored in the mode UIDatePickerModeCountDownTimer; for that mode, the date picker starts at 0:00. Setting this property does not animate the date picker by spinning the wheels to the new date and time; to do that you must use the setDate:animated: method.

日期

日期选择器显示的日期。

@property(nonatomic, retain) NSDate *date;

讨论

默认是创建 UIDatePicker 对象的日期。在 UIDatePickerModeCountDownTimer 模式下忽略日期;对于该模式,日期选择器从 0:00 开始。设置此属性不会通过将轮子旋转到新的日期和时间来为日期选择器设置动画;为此,您必须使用 setDate:animated: 方法。

You need to use it like:

你需要像这样使用它:

NSDate *chosen = [datePicker date];

回答by Anil Varghese

Do like this

这样做

- (IBAction)displayDay:(id)sender 
{

  NSDate *chosen = [self.datePicker date];

}

回答by Balu

- (IBAction)displayDay:(id)sender {

    NSDate *chosen = [yourdatepicker date];
      NSLog(@"%@",chosen);
}

try like this.

像这样尝试。