2016-12-02 63 views
0

我正在實施一個具有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 
    } 
} 

請注意,圖像名稱應與數組中的元素相匹配。

+0

我試過這個,但運行時出現線程錯誤。謝謝 –

+0

你連接了文本視圖插座嗎? – Idan

+0

感謝您的幫助,這工作。 :) –

相關問題