如何从另一个 UIPickerview 更改 UIPickerView 的字符串?

Posted

技术标签:

【中文标题】如何从另一个 UIPickerview 更改 UIPickerView 的字符串?【英文标题】:How can I change the strings of a UIPickerView from another UIPickerview? 【发布时间】:2015-09-24 19:18:48 【问题描述】:

我的视图控制器中有两个UIPickerviewpickerView1pickerView2

我想在这里实现的是,当我从pickerView1 中选择一个字符串时,它会自动更改pickerView2 的字符串。

我创建了第 3 部分,因为有很多字符串,理想情况下,我希望这一切都在第 1 部分中,因为我目前将代码设置为 3 个选择器视图,但我只想要两个选择器视图。

这是一个例子。

Pickerview1 包含 "a","b","c","d"。

如果选择“a”,pickerView2 将显示特定的字符串以供选择,如果我选择“b”,则“a”的字符串不会显示,但“b”的字符串将单独显示.

在第 2 节中,我希望这些字段中的每一个都与第 1 节和第 3 节中定义的子字符串对应,然后填充第二个 UIpickerview,所以每次我在第一个选择器视图中选择一个字符串时,第二个 uipickerview为此生成特定的字符串。

这是我的代码:

//PickerView Category IBOutlets

    @IBOutlet var pickerView1: UIPickerView! = UIPickerView()
    @IBOutlet var pickerView2: UIPickerView! = UIPickerView()
    @IBOutlet var pickerView3: UIPickerView! = UIPickerView()
    @IBOutlet var textField1: UITextField! = UITextField()
    @IBOutlet var textField2: UITextField! = UITextField()
    @IBOutlet var textField3: UITextField! = UITextField()

    var SECTION2 = ["Select", "Criminal Activity", "Begging","Harassment", "Criminal Damage", "Disruption Of Service", "Drunken Behaviour","Fare Evasion", "Feet on Seats", "Hate Crime", "Inappropriate Sexual Behaviour", "Litter", "Missile Throwing", "Noise","Rowdy Behaviour", "Smoking Tobacco", "Drugs", "Threats/abuse/intimidation towards Staff", "Threats/abuse/intimdation towards Passengers" ]

    var SECTION1 = ["Select", "*>>>Criminal Activity<<<*", "Assault", "Arson", "Trespass", "Spitting", "*>>>Begging<<<*", "On Board", "Off Board", "*>>>Harassment<<<*", "Stalking", "On Board", "At Stop/Station", "Bullying", "*>>>Criminal Damage<<<*", "Damage to Exterior", "Damage to Interior", "Glass Breakage","Grafitti", "Window Etching", "*>>>Disruption Of Service<<<*", "Interfering with the engine", "Opening emergency exits", "Pressing emergency stop/handles", "Throwing debris on to the line/track", "*>>>Drunken Behaviour<<<*", "Drinking on the mode of transport", "Drinking in the station/stop", "Aggressive Drunks", "*>>>Fare Evasion<<<*", "Fraudulent Pass", "None payment of fare", "*>>>Feet on Seats<<<*", "Purposeful Obstruction" ]

    var SECTION3 = ["Select", "*>>>Hate Crime<<<*", "Disability", "Ethnicity", "Gender", "Race","Religion", "Sexual Orientation", "*>>>Litter<<<*", "On Board", "At station/stop", "*>>>Missile Throwing<<<*", "Stone Throwing", "Throwing Objects", "*>>>Noise<<<*", "Inappropriate Musical Content", "Loud Music", "*>>>Rowdy Behaviour<<<*", "School Children", "Shouting/Swearing", "Within Shelter", "Within Station/Stop"," Groups/Gangs", "*>>>Smoking Tobacco<<<*", "On Board", "At station/stop", "*>>>Drugs<<<*", "Smoking Drugs", "Drug Paraphernalia", "Drug Taking", "Drug Dealing", "*>>>Threats/abuse/intimidation towards Staff<<<*", "On Board", "At station/stop", "*>>>Threats/abuse/intimidation towards Passengers<<<*", "On Board", "At station/stop" ]

    override func viewDidLoad() 
        super.viewDidLoad()

        var Viewcontroller: () =
        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()
        pickerView1.delegate = self
        pickerView2.delegate = self
        pickerView3.delegate = self

        self.textField1.inputView = self.pickerView1;
        self.textField2.inputView = self.pickerView2;
        self.textField3.inputView = self.pickerView3;

        pickerView1.tag = 0
        pickerView2.tag = 1
        pickerView3.tag = 2

        textField1.inputView = pickerView1
        textField2.inputView = pickerView2
        textField3.inputView = pickerView3
    

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int  
        return 1
    

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 

        if pickerView.tag == 0 
            return SECTION1.count
         else if pickerView.tag == 1 
            return SECTION2.count
         else if pickerView.tag == 2 
            return SECTION3.count
        
        return 1
    

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 

        if pickerView.tag == 0 
            return SECTION1[row]
         else if pickerView.tag == 1 
            return SECTION2[row]
         else if pickerView.tag == 2 
            return SECTION3[row]
        
        return ""
    

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  

        if pickerView.tag == 0 
            textField1.text = SECTION1[row]
         else if pickerView.tag == 1 
            textField2.text = SECTION2[row]
         else if pickerView.tag == 2 
            textField3.text = SECTION3[row]
        

    

【问题讨论】:

【参考方案1】:

设置全局变量

var data1:NSMutableArray=["Apple","Mango","Banana"]
    var data2:NSMutableArray=[["greenApple","whiteApple","yellowApple"],["greenMango","whiteMango","yelloMango"],["greenBanana","whiteBanana","yellowBanana"]]
    var data3:NSArray = []

设置pickerViews的标签——pickerView1(比如1)和pickerView2(比如2)。

     override func viewDidLoad() 
            super.viewDidLoad()
      pickerView1.tag=1;
            pickerView2.tag=2;
data3=["greenApple","whiteApple","yellowApple"] //initialise the array with the data
     

在委托方法中设置标题

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! 
    if pickerView.tag==1 
    return data1[row] as! String
    
    else 
        return data3[row] as! String
    


在选择委托中,如果选择了选择器,则根据 pickerView1 选择为 data3 分配所需的数据并重新加载 pickerView2

 func pickerView(pickerView: UIPickerView,
    didSelectRow row: Int,
    inComponent component: Int)


     if pickerView.tag==1 
        data3=data2[row] as! NSArray
        pickerView2.reloadAllComponents() // reload pickerview2
    


【讨论】:

我已经尝试了上面的代码,但是我仍然遇到错误,你能给我一个用“ViewController.swift”编写的代码的例子吗?谢谢。【参考方案2】:

如果要符合这些协议,则需要实现 UIPickerViewDataSource 和 UIPickerViewDelegate 所需的所有方法。

并为 ViewController.swift 尝试此代码

导入 UIKit 导入基金会

类 ViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate

@IBOutlet var pickerView1: UIPickerView! = UIPickerView()
@IBOutlet var pickerView2: UIPickerView! = UIPickerView()
@IBOutlet var textField1: UITextField! = UITextField()
@IBOutlet var textField2: UITextField! = UITextField()

var data1:NSMutableArray=["Apple","Mango","Banana"]
var data2:NSMutableArray=[["greenApple","whiteApple","yellowApple"],["greenMango","whiteMango","yelloMango"],["greenBanana","whiteBanana","yellowBanana"]]
var data3:NSArray = []

override func viewDidLoad() 
    super.viewDidLoad()

    pickerView1.tag=1;
    pickerView2.tag=2;
    data3=["greenApple","whiteApple","yellowApple"] //initialise the array with the data

    textField1.text = data1[0] as! String
    textField2.text = data3[0] as! String



func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int  
    return 1


func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int 
    return 3;

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!

if pickerView.tag==1 
    return data1[row] as! String

else 
    return data3[row] as! String

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent 组件: Int)

if pickerView.tag==1 
    data3=data2[row] as! NSArray
    pickerView2.reloadAllComponents() // reload pickerview2


 if pickerView.tag==1 
textField1.text = data1[row] as! String
 else
textField2.text = data3[row] as! String

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

【讨论】:

我添加了一些额外的代码来让它工作,我现在的问题是当我在点击文本字段初始化之前启动应用程序时,“Apple”和“greenApple”出现在文本字段中选择器视图。另外如何在第一个pickerview和第二个pickerview中添加其他选项?

以上是关于如何从另一个 UIPickerview 更改 UIPickerView 的字符串?的主要内容,如果未能解决你的问题,请参考以下文章

从另一个控制器的 UIPickerView 更新 UIButton 的标题

如何从另一个 QT 应用程序访问 Qt UI 的 QObject?

从另一个线程调用 CoreData ContextObjectsDidChangeNotification

如何更改 Uipickerview 位置?

如何更改 UIPickerView 中的字体大小?

如何更改 UIPickerView 选择器的颜色