从pickerView访问viewController

时间:2016-07-29 07:22:29

标签: ios swift xcode uipickerview

在视线上我有一个PickerView和一个按钮。根据pickerview中的选择(我有4行),我想点击一个按钮继续进行特定的视图。现在,无论我的选择是什么,我总是在相同的观点(生理盐水)。下面是我的控制器代码。如果有人能帮助我理解我的错误?

import UIKit

class SommaireGeneralViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var picker: UIPickerView!

    var PlacementAnswer = 0

    var liste = ["Sommaire général", "Région Est", "Région Ouest", "Région Sud", "Région Nord"]

     override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        self.picker.delegate = self
        self.picker.dataSource = self

    }

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

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

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

    func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        let titleData = liste[row]
        let myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
            return myTitle
    }

    @IBAction func Voir(sender: AnyObject) {

        if (PlacementAnswer == 0) {
            self.performSegueWithIdentifier("Saline", sender: self)


        } else if (PlacementAnswer == 1){
            self.performSegueWithIdentifier("1", sender: self)

            return

    }

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

    }

    func prepareForSegue(id: UIStoryboardSegue, sender: AnyObject?) {
        if "Saline" == id.identifier {

        }
        else if "1" == id.identifier {
        }

    }
}

2 个答案:

答案 0 :(得分:0)

你好,最后它正在工作,我把代码可以用于其他人。 干杯再次感谢您的信息

import UIKit

class SommaireGeneralViewController: UIViewController, UIPickerViewDelegate,     UIPickerViewDataSource {


@IBOutlet weak var picker: UIPickerView!

enum ActionSheetTag: Int {
    case ActionSheet0
    case ActionSheet1
}

var PlacementAnswer = Int()

let liste = ["Sommaire général", "Région Est", "Région Ouest", "Région Sud", "Région Nord"]

 override func viewDidLoad() {
    super.viewDidLoad()

    self.picker.delegate = self
    self.picker.dataSource = self

}

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

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

}



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

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int,  forComponent component: Int) -> NSAttributedString? {
    let titleData = liste[row]
    let myTitle = NSAttributedString(string: titleData, attributes:   [NSFontAttributeName:UIFont(name: "Georgia", size: 10.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
        return myTitle
}


@IBAction func Voir(sender: AnyObject) {


    if (PlacementAnswer == 0) {
        self.performSegueWithIdentifier("Saline", sender: self)


    } else if (PlacementAnswer == 1){
        self.performSegueWithIdentifier("Autre", sender: self)

    }

  }
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if let tag = ActionSheetTag(rawValue: row) {
            switch tag {
            case .ActionSheet0:
                PlacementAnswer = 0
            case .ActionSheet1:
                PlacementAnswer = 1


}
}
}
}

答案 1 :(得分:-1)

您尝试过标记和枚举吗?如果您的答案是否定的,请尝试将输出返回给我。

enum ActionSheetTag: Int {
        case ActionSheet1
        case ActionSheet2
}

。 。

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        if let tag = ActionSheetTag(rawValue: pickerView.tag) {
            switch tag {
            case .ActionSheet1:
               PlacementAnswer = 0
            case .ActionSheet2:
               PlacementAnswer = 1
            }
        }
    }

希望它有所帮助。