随机名称选择器使用带有swift的选择器视图

时间:2015-02-07 09:12:01

标签: swift xcode6

我正在尝试使用标签中的文字填充选择器视图。 我试过在网上和书中搜索如何做到这一点,但没有运气。 我到目前为止所显示的没有错误,但仍然无法运行

感谢

导入UIKit

类ViewController:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {

@IBOutlet weak var label3: UILabel!

@IBOutlet weak var label2: UILabel!

@IBOutlet weak var label: UILabel!

@IBOutlet weak var textField: UITextField!

@IBOutlet weak var textField2: UITextField!

@IBOutlet weak var textField3: UITextField!


@IBAction func button(sender: UIButton) {

    label.text = textField.text
    label2.text = textField2.text
    label3.text = textField3.text
    self.textField.resignFirstResponder()
    self.textField2.resignFirstResponder()
    self.textField3.resignFirstResponder()
}

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return false
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    self.view.endEditing(true)
}


var pickerLabels: [UILabel!] {

    return [label,label2,label3]
}

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component:Int) -> Int{
    return pickerLabels.count
}

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

return pickerLabels

}

override func viewDidLoad() {
   super.viewDidLoad()
   // 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.

}

}

1 个答案:

答案 0 :(得分:0)

您正在向您的阵列发送UILabels。 我相信您要实现的目标需要您从UILabels获取文本,并在计算属性上将数组格式化为[String]

var pickerLabels: [String!] {

    return [label.text, label2.text, label3.text]
}