UIDatePicker with default nil value - iOS

UIDatePicker with default nil value - iOS



I have searched a lot but found no solution. Anyone can you please help me to find out how to set UIDatePicker initial value as empty textfield??


import UIKit
import Alamofire
import KVNProgress

class EditProfileVC: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate

@IBOutlet var datePickerView: UIView!
@IBOutlet var datePicker: UIDatePicker!


@IBOutlet var birthdayText: UnderLineTextField!
@IBOutlet var bioText: UnderLineTextField!

@IBOutlet var scrollView: UIScrollView!
var imagePicker = UIImagePickerController()

var photoFlag = -1

let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)

var singleTonInstance = SingleTon.sharedDataContainer

var email = ""
var token = ""

override func viewDidLoad()

super.viewDidLoad()

self.birthdayText.text = ""
if #available(iOS 11.0, *)

navigationController?.navigationBar.prefersLargeTitles = false

else

// Fallback on earlier versions


SwiftyPlistManager.shared.getValue(for: "access_token", fromPlistWithName: "AccountDetails") (result, err) in

if err == nil

self.token = (result as? String)!



activityView.color = UIColor.blue
activityView.center = self.view.center

self.view.addSubview(activityView)

self.datePickerView.isHidden = true

self.genderTxt.isUserInteractionEnabled = false
self.birthdayText.isUserInteractionEnabled = false

imagePicker.delegate = self

datePicker.addTarget(self, action: #selector(datePickerChanged), for: .valueChanged)

self.getProfileDetails()




@objc func savePressed()

self.activityView.isHidden = false
self.activityView.startAnimating()

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMMM dd, YYYY"
let date = dateFormatter.date(from: self.birthdayText.text!)
dateFormatter.dateFormat = "dd-MM-yyyy"
let dob = dateFormatter.string(from: date!)

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let urlString = appDelegate.serverUrl + "edit_profile?full_name=(self.fullNameText.text!)&dob=(dob)&gender=(self.genderTxt.text!)&bio_data=(self.bioText.text!)&location=(self.locationText.text!)&web_site=(self.webTextField.text!)&token=(self.token)"





@objc func datePickerChanged(sender: UIDatePicker)

let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, YYYY"
birthdayText.text = formatter.string(from: sender.date)

override func didReceiveMemoryWarning()

super.didReceiveMemoryWarning()

func getProfileDetails()

self.activityView.isHidden = false
self.activityView.startAnimating()

let appDelegate = UIApplication.shared.delegate as! AppDelegate

let urlString = appDelegate.serverUrl + "view_profile?token=(self.token)"



}

@IBAction func birthdayTapped(_ sender: Any)

self.datePickerView.isHidden = false

}



The above code is my whole viewcontroller class code anyone can you please help me to find out the solution please.I assign the value of birthdayText as nil I ViewController but no use.





Is there a reason there cannot be a default date? Using the current Day or a fix date such as 1st Jan 2000 is quite common and understood.
– Ben Ong
Jul 6 at 7:49





Why do you want to do that? A Picker, is a UI component that is easy to handle (for the user), a quick "scroll" and a limited pool of possibilities. Why use a UITextField then? Or is that were you put the selected date? Then use a Label in order to disable the modification?
– Larme
Jul 6 at 7:50






No when vieweDidLoad I should have an empty textfield. and it should show the date only after select a date from date picker.
– Angel F Syrus
Jul 6 at 7:51





can you explain "UIDatePicker initial value as empty textfield??" it must be otherway around?
– Iraniya Naynesh
Jul 6 at 7:52





No the textfield should show the date only after select a date from date picker.
– Angel F Syrus
Jul 6 at 7:52




2 Answers
2



A UITextField's text property is empty by default. So the following code works as expected. Compare it to your code:


UITextField


text


class ViewController: UIViewController

private let textField: UITextField =
let tf = UITextField()
tf.borderStyle = .roundedRect
tf.placeholder = "Choose date and time"
tf.text = "" // not even necessary
return tf
()

lazy private var datePicker: UIDatePicker =
let dp = UIDatePicker()
dp.addTarget(self, action: #selector(datePickerValueChanged), for: .valueChanged)
return dp
()

lazy private var dateFormatter: DateFormatter =
let df = DateFormatter()
df.dateStyle = .short
df.timeStyle = .short
return df
()

@objc private func datePickerValueChanged(datePicker: UIDatePicker)
textField.text = dateFormatter.string(from: datePicker.date)


override func viewDidLoad()
super.viewDidLoad()

let stackView = UIStackView(arrangedSubviews: [textField, datePicker])
stackView.axis = .vertical

view.addSubview(stackView)
stackView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
stackView.topAnchor.constraintEqualToSystemSpacingBelow(view.safeAreaLayoutGuide.topAnchor, multiplier: 1),
stackView.leadingAnchor.constraintEqualToSystemSpacingAfter(view.safeAreaLayoutGuide.leadingAnchor, multiplier: 1),
view.safeAreaLayoutGuide.trailingAnchor.constraintEqualToSystemSpacingAfter(stackView.trailingAnchor, multiplier: 1)
])





Results in:



result



Update:



Your problem seems to be that you call self.getProfileDetails() at the end of viewDidLoad. In that method you have the line self.birthdayText.text = dateFormatter.string(from: date!) that sets the text.


self.getProfileDetails()


viewDidLoad


self.birthdayText.text = dateFormatter.string(from: date!)





What does that mean?
– André Slotta
Jul 6 at 8:57





@AngelFSyrus Check my updated answer...
– André Slotta
Jul 6 at 9:01





Thanks I also found my mistake. Actually this is not my project my role is only for debugging.So that only little confusion about the code. Thanks again
– Angel F Syrus
Jul 6 at 9:27



i didnt try this in my xcode try this


@objc func datePickerChanged(sender: UIDatePicker)

let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, YYYY"
birthdayText.text = ""





but after selecting the value the the textfield should have the value of date picker.
– Angel F Syrus
Jul 6 at 8:07





what is the birthdaytext? is it textview? get the selected date from pickerview and when you set to textfield send it like this ""
– gihanshox
Jul 6 at 8:15





birthdayTextis a textField.
– Angel F Syrus
Jul 6 at 8:18






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ