具有默认 nil 值的 UIDatePicker - iOS

UIDatePicker with default nil value - iOS

我已经搜索了很多但没有找到解决方案。谁能帮我找出如何将 UIDatePicker 初始值设置为空文本字段?

 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
        }
    }

以上代码是我的全部viewcontrollerclass代码谁能帮我找出解决方案please.I将birthdayText的值赋为nil我ViewController 但没用。

我没有在我的 xcode 中尝试过这个

@objc func datePickerChanged(sender: UIDatePicker)
{
    let formatter = DateFormatter()
    formatter.dateFormat = "MMMM dd, YYYY"
    birthdayText.text = ""
}

AUITextFieldtext属性默认为空。所以下面的代码按预期工作。将其与您的代码进行比较:

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)
        ])
    }

}

结果:

更新:

您的问题似乎是您在 viewDidLoad 的末尾调用了 self.getProfileDetails()。在该方法中,您有设置文本的行 self.birthdayText.text = dateFormatter.string(from: date!)