xcode 如何在日期选择器上以编程方式设置日期?setDate 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7370400/
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-09-14 21:54:45  来源:igfitidea点击:

How can I set Date programmatically on Date Picker? setDate is not working?

iphoneobjective-cxcodedatepickeruidatepicker

提问by Arpit B Parekh

I am using Xcode 4.

我正在使用 Xcode 4。

I have DatePicker Component.

我有 DatePicker 组件。

It is taking the Default date which is set in IB's Date to highlight and select the date...

它采用在 IB 的日期中设置的默认日期来突出显示并选择日期...

I want to set the current date in date picker to be selected...

我想在要选择的日期选择器中设置当前日期...

Not working setDate Method...

不工作 setDate 方法...

 - (void)viewDidLoad
    {
        [super viewDidLoad];   

        [ddlDatePicker setDate:[NSDate date]];

        NSLog(@"-%@-",[NSDate date]);
     //NSLog is showing me the current date..Today's Date...
    //But ddlDatePicker is setted to the date on the day on which I created the date 
//Picker...Or You can say date field in the DatePicker's IB Properties.. 

    ![enter image description here][1]
    }

enter image description here

在此处输入图片说明

回答by Nekto

You have not connected with outletyour ddlDatePicker, so you haven't got reference to that object and couldn't manage it.

您没有连接到outlet您的ddlDatePicker,因此您没有对该对象的引用并且无法管理它。

Open IB and connect this UIDatePickerwith your variable in appropriate controller.

打开 IB 并将其UIDatePicker与适当控制器中的变量连接。

回答by Nathanial Woolls

Try this in viewWillAppear rather than viewDidLoad. Also, ensure ddlDatePicker is not nil:

在 viewWillAppear 而不是 viewDidLoad 中试试这个。另外,确保 ddlDatePicker 不为零:

NSLog(@"%@", ddlDatePicker);