我创建了一个自定义选择器视图作为带有完成按钮和所有内容的键盘,但是我在确定如何从一开始就立即加载数据时遇到了一些麻烦,我在我的日期选择器中做了,但现在我'我试图弄清楚如何在我定制的普通选择器键盘中做到这一点。
这是我的代码:
import UIKit
import CoreData
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, NSFetchedResultsControllerDelegate {
@IBOutlet weak var stateText: UITextField!
@IBOutlet weak var taxNumb: UITextField!
@IBOutlet weak var output: UILabel!
func doneButton() {
let doneToolbar : UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 50))
doneToolbar.barStyle = UIBarStyle.Default
let flexSpace = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
let done : UIBarButtonItem = UIBarButtonItem(title: "Done", style: .Done, target: self, action: ("doneButtonAction"))
var items = [UIBarButtonItem]()
items.append(flexSpace)
items.append(done)
doneToolbar.items = items
doneToolbar.sizeToFit()
self.stateText.inputAccessoryView = doneToolbar
self.taxNumb.inputAccessoryView = doneToolbar
}
func doneButtonAction() {
self.stateText.resignFirstResponder()
self.taxNumb.resignFirstResponder()
}
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let tax : Tax? = nil
var stateArray = [
"AK - Alaska",
"AL - Alabama",
"AR - Arkansas",
"AZ - Arizona",
"CA - California",
]
func statePickerKey() {
let statePickerView = UIPickerView()
statePickerView.dataSource = self
statePickerView.delegate = self
stateText.inputView = statePickerView
}
override func viewDidLoad() {
super.viewDidLoad()
statePickerKey()
doneButton()
}
override func viewDidAppear(animated: Bool) {
statePickerKey()
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return stateArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return stateArray[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
return stateText.text = stateArray[row] as String
}
@IBAction func printTapped(sender: AnyObject) {
let entityDescription = NSEntityDescription.entityForName("Tax", inManagedObjectContext: moc)
let taxes = Tax(entity: entityDescription!, insertIntoManagedObjectContext: moc)
taxes.state = stateText.text
if stateText.text == "AK - Alaska" {
taxes.alaska = Double(taxNumb.text!)
output.text = taxes.alaska?.stringValue
} else if stateText.text == "AL - Alabama" {
taxes.alabama = Double(taxNumb.text!)
self.output.text = taxes.alabama?.stringValue
} else if stateText.text == "AR - Arkansas" {
taxes.arkansas = Double(taxNumb.text!)
output.text = taxes.arkansas?.stringValue
} else if stateText.text == "AZ - Arizona" {
taxes.arizona = Double(taxNumb.text!)
output.text = taxes.arizona?.stringValue
} else if stateText.text == "CA - California" {
taxes.california = Double(taxNumb.text!)
output.text = taxes.california?.stringValue
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
你创建了datepicker,之后设置了textfield.text = datePickerView.date