Default Date for UIDatePicker in iOS
I am new in iOS development. I have this ViewController in my Storyboard (includes a UIDatePicker and a bar button):
I followed Data Cell to call my DatePicker. When user click on a button in the page PickerDate pops up with a done bar button on the navigation bar:
The problem is it shows just the current date as
default. Nothing changes when I set it from Properties:
Do you know how can I fix it? (by
setting up the properties or programmatically).
- Keep single storyboard for iPhone and iPad
- Text Field Should Return, is this correct for ios7?
- How to Remove the Warning “Frame for Button will be different at run time.”
- Fails to distribute my app: “Your app contains non-public API usage.”
- Delete button not showing on UITableViewCell with rounded corners
- What's the right way to parse an ISO8601 date in cocoa?
3 Solutions Collect From Internet About “Default Date for UIDatePicker in iOS”
I did it programmatically in this way :
NSCalendar *calendar = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease]; NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease]; [components setYear:1980]; [components setMonth:1]; [components setDay:1]; NSDate *defualtDate = [calendar dateFromComponents:components]; pickerView.date = defualtDate;
But still don’t know why it doesn’t work by
setting up the properties. Any help?
let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)! // let calendar: NSCalendar = NSCalendar.currentCalendar() calendar.timeZone = NSTimeZone(name: "UTC")! let components: NSDateComponents = NSDateComponents() components.year = 1980 components.month = 1 components.day = 1 let defaultDate: NSDate = calendar.dateFromComponents(components)! self.dateOfBirthUIDatePicker.date = defaultDate
Great answers above – Here’s the Swift3 (Feb 17) version:
let calendar: NSCalendar = NSCalendar(calendarIdentifier: .gregorian)! calendar.timeZone = NSTimeZone(name: "UTC")! as TimeZone let components: NSDateComponents = NSDateComponents() components.year = 1980 components.month = 1 components.day = 1 let defaultDate: NSDate = calendar.date(from: components as DateComponents)! as NSDate self.datePicker.date = defaultDate as Date
- UICollectionView cellForItemAtIndexPath is nil
- UIActivityViewController in Swift Crashes on iPad
- How to upload image that was taken from UIImagePickerController
- UITableView with infite scrolling and lazy loading
- Xcode will run app on simulator but not on device
- With ARC, what's better: alloc or autorelease initializers?
- iOS overflow-x (or position absolute) makes scrolling choppy?
- Reference to 'X' is ambiguous
- Firebase – how to get the key value in observeEventType = Value
- iOS10 – ContentView Blocking Touches of UIButton in UITableView Header
- WatchKit Complication: get Complication data from extension delegate
- why is my didEnterRegion delegate not called?
- Swift, UIView touch event in controller
- How to add loading Indicator in Footer of CollectionView in iOS
- Setting the App Icon (Xcode 7)