viewDidLoad的重新声明无效

时间:2017-02-17 17:47:06

标签: ios viewdidload

我的代码是:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBOutlet weak var statePicker: UIPickerView!
    @IBOutlet weak var statePickerBTN: UIButton!

    let states = ["Alaska,Arkansas, Alabama, California, Maine, New York"]

    override func viewDidLoad() {
        super.viewDidLoad()
        statePicker.dataSource = self
        statePicker.delegate = self
    }

    @IBAction func statePickerButton(_ sender: Any) {
    }

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

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

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

它说它是“无效重新声明'viewDidLoad()'

2 个答案:

答案 0 :(得分:0)

你写了这个函数

 override func viewdidLoad() {
      super.viewdidLoad()
 }

两次代码。去掉它。它会得到解决

答案 1 :(得分:0)

我认为您是Swift和iOS的新手。您的错误清楚地表明""' viewDidLoad()'""'无效重新声明你已经将方法viewDidLoad声明了两次。删除一个。我们不能在类中有多个具有相同名称和参数的方法。

override func viewDidLoad() {
        super.viewDidLoad()
        statePicker.dataSource = self
        statePicker.delegate = self
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }