在选择器视图中选择项目后需要显示文本视图

Posted

技术标签:

【中文标题】在选择器视图中选择项目后需要显示文本视图【英文标题】:Need to display a text view after selecting an item in picker view 【发布时间】:2016-12-02 12:04:57 【问题描述】:

我正在实现一个具有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.
    
    */

【问题讨论】:

如果您从选取器视图中选择特定项目,我希望它显示与标签类似的不同文本视图。 【参考方案1】:

这是一种选择:

如果您选择使用此选项,我会考虑传递鸡尾酒数组。 可能有更简洁的选项,例如使用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 
    

请注意,图像名称应与数组中的元素匹配。

【讨论】:

我试过这个,但运行时出现线程错误。谢谢 你连接文本视图插座了吗? 如何添加图片

以上是关于在选择器视图中选择项目后需要显示文本视图的主要内容,如果未能解决你的问题,请参考以下文章

SwiftUI 选择器为所选项目和选择视图分隔文本

浓缩咖啡:为啥旋转器在选择后不关闭?

在活动一中选择的微调项目上,必须更改第二个活动中的文本视图

如何设置选择器视图以显示来自多个矩阵选项的数据?

为啥我的选择器视图没有出现在某些文本字段上?

如何使用 UIPickerView 在一个视图中填充不同的文本字段?