我正在實施一個具有UIPickerView
的應用程序。一旦選擇器視圖被選中,然後將數據傳遞到下面的視圖控制器,名爲RecipeViewController
。該標籤在視圖控制器上顯示從拾取器視圖中選擇的項目,但是現在我需要一個文本視圖來顯示。我將如何實現這個到我的代碼下面有兩個類?在選擇器視圖中選擇一個項目後需要顯示文本視圖
import UIKit
class PickerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var cocktailpicker: UIPickerView!
var cocktails = ["Mojito","Long Island Iced Tea","Sex On The Beach","Screaming Orgasum","Cosmo","Strawberry Summer Sling","Rosini Royal","Blue Lagoon"]
var passdata: String = ""
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.cocktailpicker.delegate = self
self.cocktailpicker.dataSource = self
passdata = cocktails[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return cocktails.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return cocktails[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
passdata = cocktails[row]
//cocktails[pickerView.selectedRowInComponent(0)]
print(cocktails[row])
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// 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.
}
*/
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "seguepicker") {
if let rvc: RecipeViewController = segue.destinationViewController as? RecipeViewController {
rvc.toPass = passdata
}
//var rvc = segue.destinationViewController as RecipeViewController;
//rvc.toPass = selectedValue
}
}
}
import UIKit
class RecipeViewController: UIViewController {
var toPass:String!
@IBOutlet weak var labelPassedData: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
labelPassedData.text = toPass
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// 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.
}
*/
}
如果您從選取器視圖中選擇特定項目,我希望它顯示與標籤類似的不同文本視圖。 –