1
import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { @IBOutlet var picker: UIPickerView! @IBOutlet var label: UILabel!

var Array = ["Iphone", "겔럭시", "베가아이언"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    picker.delegate = self
    picker.dataSource = self

}

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

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

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
@IBAction func buttonTapped(sender: AnyObject) {
                    label.text = Array[row]       ///this part error "use of unresolved identifier row"


}

}

could you explain to me the difference between the error row and row that I used for title above?

why I would get error for the bottom one. I declare correctly I think.

0

1 Answer 1

1

(I am assuming you want the label's text to change when the user taps a button)

This function runs when the pickerView's value has changed(the row parameter that you were trying to use, runs when the pickerView is being made, not when the value has changed)-

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

Also, you want to create a global variable so you can use row(the row parameter in the didSelectRow function can only be used inside that function) in your IBAction function.

So add this piece of code under the class definition-

var rowSelected = 0

Next, add this function to your code -

 func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
      rowSelected = row
}

And finally, change the label.text = Array[row] to label.text = Array[rowSelected](this should also make your unresolved identifier error dissappear) .

So your whole code should look like this-

var rowSelected = 0
var Array = ["Iphone", "겔럭시", "베가아이언"]

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    picker.delegate = self
    picker.dataSource = self

}

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

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

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
    return 1
}
@IBAction func buttonTapped(sender: AnyObject) {
       label.text = Array[rowSelected]


}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
      rowSelected = row
}

Hopefully that worked out.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for very detail explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.