根据点击的字段将数据加载到UIPIckerView

时间:2015-04-27 18:31:30

标签: ios uidatepicker

当用户点击视图中的不同字段时,我正试图找到一种方法将数据加载到我的PickerView中。当我完成视图的其他部分时,我很早就开始工作了,这已经不再适用了。现在硬编码数据以获得项目的功能,然后从我们的主服务器上生成的包含此用户特定数据的XML文件中提取数据。

现在我只是想让选择器适用于一个字段(enterDispTo),但最终代码需要适用于视图中的三个非日期字段。

这是我目前的代码。对不起,它太长了,但我不想遗漏可能是问题的东西。这对我来说很新鲜。

import UIKit

class DispenseScreen1ViewController:UIViewController,UIPickerViewDelegate {

// keep your outlets at the top
@IBOutlet var myDatePicker: UIDatePicker!
@IBOutlet var userLabel: UILabel!
@IBOutlet var teamLabel: UILabel!
@IBOutlet var enterDate: UITextField!
@IBOutlet var enterSeason: UITextField!
@IBOutlet var enterSport: UITextField!
@IBOutlet var enterDispTo: UITextField!

// put variables and constants here
let today = NSDate()
var season = traSeason
var sport = traSport
var dispensedToSelector = dispensedTo
var activeField = []
var datePickerView = UIDatePicker()
var fieldDataPickerView = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()

    userLabel.text = userName
    teamLabel.text = teamName

    // initialize the inputView of the transaction date field to a datePickerView. This will allow the date picker to appear when the field is tapped
    enterDate.inputView = datePickerView

    // this limits the date picker to date only
    datePickerView.datePickerMode = UIDatePickerMode.Date
    datePickerView.date = NSDate()
    enterDate.text = NSDate().formatted
    datePickerView.addTarget(self, action: "dateChangedAction", forControlEvents: UIControlEvents.ValueChanged)

    //initailize the inputView of the season field to the PickerView. This will allow the picker to appear when the field is tapped
    enterSeason.inputView = fieldDataPickerView

    if season.count > 0 {
        enterSeason.text = season[0] as! String //value in [] will be a var that sets default season
    }

    //initailize the inputView of the season field to the PickerView. This will allow the picker to appear when the field is tapped
    enterSport.inputView = fieldDataPickerView

    if sport.count == 1 {

        enterSport.text = sport[0] as String //need to convert to a string
        enterSport.enabled = false //do not allow editing

    } else {
        //do nothing
    }

    //initailize the inputView of the enterDispTo field to the PickerView. This will allow the picker to appear when the field is tapped
    enterDispTo.inputView = fieldDataPickerView

    enterDispTo.text = dispensedToSelector[0]

}

func dateChangedAction() {
    datePickerView.maximumDate = NSDate()
    enterDate.text = datePickerView.date.formatted
}

//func fieldChangedAction() {
   // enterDispTo.text = self.itemSelected
//}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

//load data into PickerView and return the value selected
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
    }
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return self.activeField.count
    }
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return self.activeField[row] as! String
    }
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    var itemSelected = self.activeField[row] as! String
    }

//function to hide keyboard when return key is tapped
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

//funtion to hide keyboard when screen is tapped
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    self.view.endEditing(true)
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/

@IBAction func seasonTapped(sender: AnyObject) {
    //code for what to do when season field is tapped
    activeField = season
}


@IBAction func sportTapped(sender: AnyObject) {
    //code for what to do when sport field is tapped
    activeField = sport
}


@IBAction func dispToTapped(sender: AnyObject) {
    //code for what to do when dispTo is tapped
    activeField = dispensedToSelector
    //fieldDataPickerView.addTarget(self, action: "fieldChangedAction", forControlEvents: UIControlEvents.ValueChanged)
}

} extension NSDate { var formatted: String { let df = NSDateFormatter() df.dateFormat = "dd MMM yyyy" df.locale = NSLocale(localeIdentifier: "en_us_POSIX") return df.stringFromDate(self) } }

0 个答案:

没有答案