访问其他viewcontroller中的变量 - swift

时间:2015-02-25 14:48:05

标签: swift

我想在第二个viewcontroller中访问变量answerdata。我很快就开始了,我尝试了很多选择,但没有任何效果。

第一个viewcontroller中的代码:

import UIKit

class prvnidotaz: UIViewController {

var pickerdata = ["Small","Big","Medium"]


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

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


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

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
      var answerdata = pickerdata [row]
        println(answerdata)
    }



    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 :(得分:1)

您的var answerdata被定义为局部变量。

如果您将其定义为实例变量(也称为“属性”),则可以从第二个控制器访问answerdata。