ios - 从选择器 View 访问 viewController

标签 ios swift xcode uipickerview

一眼看去,我有一个 PickerView 和一个按钮。根据 pickerview 中的选择(我有 4 行),我想单击一个按钮进入特定 View 。现在,无论我的选择是什么,我总是持相同观点(生理盐水)。下面是我的 Controller 代码。如果有人可以帮助我理解我的错误?

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 {
        }

    }
}

最佳答案

你好,终于可以用了,我放了可以给别人用的代码。 干杯,再次感谢您提供的信息

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


}
}
}
}

关于ios - 从选择器 View 访问 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38652824/

相关文章:

swift - 如何只观察 Firebase 中的最后一个字符

iphone - 如何在swift项目中传递值并在swift中访问自己创建的框架功能

ios - 在同一个 View Controller 上使用 Swift 中的 2 个 pickerviews 时,我只看到 ?-s,如何解决这个问题?

Xcode 9.3 Playground - 诊断

iphone - 如何以编程方式将 UIButton 调整为文本大小并保持漂亮的填充?

ios - 将文本标签添加到绘制的形状

iOS官方待办教程: List View Controller does not display correctly

xcode - 如何从 Xcode 5.02 运行 C++

ios - 为什么我点击 UISwitch ,它不起作用?

iphone - 滚动时调整 UICollectionView 的大小