我想隱藏我的datepicker,直到我的textField被選中。我只是爲我的pickerView做了這個。但是,我一直無法找到一個解決方案,使用Swift顯示然後將datePicker隱藏在textField上。有沒有辦法在Swift上隱藏UIDatePicker?
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var TeeTimes: UILabel!
@IBOutlet weak var textFieldDate: UITextField!
@IBOutlet weak var datePicker: UIDatePicker!
@IBOutlet weak var picker: UIPickerView! = UIPickerView()
@IBOutlet weak var textfieldCourse: UITextField!
var pickerData = ["Norman Course", "Riding Golf", "Dmob Course"]
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
picker.hidden = true;
self.textfieldCourse.delegate = self
self.picker.delegate = self
self.picker.dataSource = self
textfieldCourse.text = pickerData[0]
//Inputs the data into the array
datePicker.hidden = true;
datePicker.addTarget(self, action: Selector("datePickerChanged:"), forControlEvents: UIControlEvents.ValueChanged)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) ->Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) ->Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
textfieldCourse.text = pickerData[row]
picker.hidden = true
}
func textFieldShouldBeginEditing(textfield: UITextField) ->Bool {
picker.hidden = false
return false
}
func datePickerChanged(dataPicker: UIDatePicker) {
datePicker.hidden = false
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = NSDateFormatterStyle.ShortStyle
let strDate = dateFormatter.stringFromDate(datePicker.date)
textFieldDate.text = strDate
}
}
嗨丹尼斯,如果我有更多的日期選擇器下面的輸入字段,有沒有辦法讓它隱藏和/或摺疊?現在在viewDidLoad()我設置了datePicker.hidden = true,但它只是留空。下面的字段沒有調整到佔用這個空間。謝謝 – rockhammer
嗨rockhammer,你的問題似乎與你設置你的UI約束的方式。您應該在Auto Layout中擺弄,直到獲得所需的結果。這應該是一個有用的參考:http://stackoverflow.com/questions/19561269/autolayout-with-hidden-uiviews –
嗨丹尼斯,感謝指針。我拼命地試圖避免顯示/隱藏datePicker的UITableView方法。你的參考幫助我弄清楚如何去做。謝謝 – rockhammer