software setting DatePicker time to the current time

I am using the alarm function in the iphone application. Here, when I set the uiswitch state to β€œON” and select a specific time from the DatePicker, until the user changes the uiswitch state to off, the datepicker should show the selected time, and when the state changes to off, the date select switch should appear. current time not previously selected. I cannot find any option to set the DatePickers time value to the current time programmatically.

+6
source share
3 answers

setDate: method used to set a specific date

 [yourDatePicker setDate:[NSDate date]]; 

It will set the date and time of selection for the current date and time.

+16
source

Try it,

ViewController.h

 @interface ViewController : UIViewController { IBOutlet UISwitch *onOffSwitch; UIDatePicker *myPicker; } -(IBAction)onOffSwitch:(id)sender; @end 

ViewController.m

  - (void)viewDidLoad { [super viewDidLoad]; CGRect pickerFrame = CGRectMake(0,250,0,0); myPicker = [[UIDatePicker alloc] initWithFrame:pickerFrame]; [self.view addSubview:myPicker]; [myPicker release]; } -(IBAction)onOffSwitch:(id)sender{ if(onOffSwitch.on) { NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000"; // Convert string to date object NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"]; NSDate *date = [dateFormat dateFromString:dateStr]; [myPicker setDate:date]; } else { [myPicker setDate:[NSDate date]]; } } 
+2
source

In Swift:

 yourDatePicker.setDate(date: NSDate, animated: Bool) 
+1
source

Source: https://habr.com/ru/post/948185/


All Articles