我是iOS开发人员的一个完整的初学者,我想创建一个小的iOS应用程序。在此应用程序上,应该假设3个UIPickerViews显示不同的数据。

我的问题在显示器上。我曾经在Android或Windows手机上进行开发,但是我不明白如何用不同的数据填充UIPickerViews

这是我已经编写的代码:

//
//  ViewController.swift
//  iphoneVersion
//
//  Created by fselva on 13/05/2015.
//  Copyright (c) 2015 fselva. All rights reserved.
//

import UIKit



class ViewController: UIViewController, UIPickerViewDelegate{



@IBOutlet weak var pickerView1: UIPickerView!
@IBOutlet weak var pickerView2: UIPickerView!
@IBOutlet weak var pickerView3: UIPickerView!

var test = ["Todo","Waiting","Maybe","Inbox","Note"]

var test2 = ["@Office","@Computer","@Home","@Meeting", "@Read", "@Achat", "@Call"]



override func viewDidLoad() {
    super.viewDidLoad()
    pickerView1.tag = 1
    pickerView2.tag = 2
    pickerView3.tag = 3
    // Do any additional setup after loading the view, typically from a nib.
}

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

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
    if pickerView2.tag == 2 {
        return test.count
    } else if pickerView3 == 3{
        return test2.count
    }
    return 1
}

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

    if pickerView2 == 2 {
        return test[row]
    } else if pickerView3 == 3{
        return test2[row]
    }
        return ""
}


}
@IBOutlet weak var pickerView1: UIPickerView!已通过Ctrl + Click从 Storyboard 自动创建到ViewController.swift。

当前,第一个UIPickerView应该什么也不显示。第二个应该显示test,第三个要显示test2

经过数小时的互联网调查后,我听说了用于定义谁必须显示内容的标签,但是该标签不起作用。

我做错什么了吗,我在某处错过了什么?

最佳答案

您不需要标签,只需使用:

 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        if pickerView == pickerView1 {
            //pickerView1
        } else if pickerView == pickerView2{
           //pickerView2
        }

也不要忘记在IB或代码中设置委托(delegate):
pickerView1.delegate = self

关于ios - 同一UIView中有多个UIPickerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30238224/

10-08 21:45