关于 swift:UIDatePicker,默认值为 nil – iOS

UIDatePicker with default nil value – iOS

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
 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
        }
    }

上面的代码是我的整个viewcontroller类代码任何人都可以请你帮我找出解决方案。我将birthdayText的值分配为nil I ViewController但没有用。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)
        ])
    }

}

结果:

result

更新:

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


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

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


原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/268703.html

(0)
上一篇 2022年6月20日
下一篇 2022年6月20日

相关推荐

发表回复

登录后才能评论