我正在实施一个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.
}
*/
}
答案 0 :(得分:0)
这是一个选项:
如果您选择使用此选项,我会考虑通过鸡尾酒阵列。 可以有更清晰的选项,例如使用enum。
class RecipeViewController: UIViewController {
var toPass:String!
var cocktails = ["Mojito","Long Island Iced Tea","Sex On The Beach","Screaming Orgasum","Cosmo","Strawberry Summer Sling","Rosini Royal","Blue Lagoon"]
@IBOutlet weak var labelPassedData: UILabel!
@IBOutlet weak var recipeTextView: UITextView!
@IBOutlet weak var cocktailImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
if let image = UIImage(named: "Mojito.png") {
cocktailImageView.image = image
} else {
print("Image is missing")
}
labelPassedData.text = toPass
// Do any additional setup after loading the view.
switch toPass {
case cocktails[0]:
recipeTextView.text = "Lime, sugar, mint..."
case cocktails[1]:
recipeTextView.text = "Long Island ingredients..."
default:
recipeTextView.text = ""
}
if let
}
}
请注意,图像名称应与数组中的元素匹配。