我似乎无法让我的两个UITextView
正确使用我的UIPickerView
与Swift合作。
我尝试使用标签来区分我的TextView,但我的第二个TextView无法正确更新。
有人能指出我正确的方向吗?
//
// 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
}
}
答案 0 :(得分:1)
将此属性添加到您的班级:
var activeTextView: UITextView?
在textViewShouldBeginEditing
设置activeTextView
:
func textViewShouldBeginEditing(textView: UITextView) -> Bool{
teamNamesPicker.hidden = false
activeTextView = textView
return false
}
在pickerView:didSelectRow:inComponent:
中使用activeTextView
更新正确的文字视图:
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int) {
activeTextView?.text = teamNamesArray[row]
teamNamesPicker.hidden = true
return
}