Newby
Newby

Reputation: 37

Multiple TextViews with one Picker

I can't seem to get my two UITextViews working with my UIPickerView correctly with Swift.

I tried using tags to differentiate between my TextViews but my second TextView will not update correctly.

Can anyone point me in the right direction?

//
//  ViewController.swift


import UIKit

class ViewController: UIViewController, UITextViewDelegate, UIPickerViewDelegate {

    @IBOutlet weak var homeTeamNameTextView: UITextView!
    @IBOutlet weak var awayTeamNameTextView: UITextView!

    @IBOutlet weak var teamNamesPicker: UIPickerView! = UIPickerView()

    // Array of teams
    let teamNamesArray = ["Arsenal", "Aston Villa", "Burnley", "Chelsea", "Crystal Palace", "Everton", "Hull City", "Leicester City", "Liverpool", "Manchester City", "Manchester United", "Newcastle United", "Queens Park Rangers", "Southampton", "Stoke City", "Sunderland", "Swansea City", "Tottenham Hotspur", "West Bromwich Albion", "West Ham United"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        teamNamesPicker.hidden = true;
        teamNamesPicker.delegate = self
        homeTeamNameTextView.text = teamNamesArray[0]
        homeTeamNameTextView.delegate = self
        awayTeamNameTextView.text = teamNamesArray[1]
        awayTeamNameTextView.delegate = self
    }

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


    // returns the number of 'columns' to display.
    func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int{
        return 1
    }

    // returns the # of rows in each component..
    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int{
        return teamNamesArray.count
    }

    func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
        return teamNamesArray[row]
    }

    func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
        if homeTeamNameTextView.tag == 0 {
            homeTeamNameTextView.text = teamNamesArray[row]
            teamNamesPicker.hidden = true;
        } else if awayTeamNameTextView.tag == 1 {
            awayTeamNameTextView.text = teamNamesArray[row]
            teamNamesPicker.hidden = true;
        }
        return
    }

    func textViewShouldBeginEditing(textView: UITextView) -> Bool{
        teamNamesPicker.hidden = false
        return false
    }


}

Upvotes: 1

Views: 905

Answers (1)

vacawama
vacawama

Reputation: 154603

  1. Add this property to your class:

    var activeTextView: UITextView?
    
  2. In textViewShouldBeginEditing set activeTextView:

    func textViewShouldBeginEditing(textView: UITextView) -> Bool{
        teamNamesPicker.hidden = false
        activeTextView = textView
        return false
    }
    
  3. In pickerView:didSelectRow:inComponent: use activeTextView to update the right text view:

    func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
        activeTextView?.text = teamNamesArray[row]
        teamNamesPicker.hidden = true
        return
    }
    

Upvotes: 1

Related Questions