我进行了很多搜索,但没有找到解决方案。有人可以帮我看看如何将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
}
}
上面的代码是我整个viewcontroller类的代码,任何人都可以请我帮忙找出解决方案。我将BirthdayText的值设置为n,而ViewController却没有用。
答案 0 :(得分:4)
UITextField
的{{1}}属性默认为空。因此,以下代码按预期工作。将其与您的代码进行比较:
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)
])
}
}
的末尾致电self.getProfileDetails()
。在这种方法中,您可以使用viewDidLoad
行来设置文本。
答案 1 :(得分:1)
我没有在我的xcode中尝试这个
@objc func datePickerChanged(sender: UIDatePicker)
{
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, YYYY"
birthdayText.text = ""
}