首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从pickerView访问viewController

从pickerView访问viewController
EN

Stack Overflow用户
提问于 2016-07-29 15:22:29
回答 2查看 215关注 0票数 1

一看到我就有一个PickerView和一个按钮。根据选择器视图中的选择(我有4行代码),我想单击一个按钮以转到特定的视图。现在,无论我的选择是什么,我总是处于相同的观点(生理盐水)。下面是我的控制器代码。有没有人能帮我理解我的错误?

代码语言:javascript
复制
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 {
        }

    }
}
EN

回答 2

Stack Overflow用户

发布于 2016-08-07 18:02:09

您好,终于可以工作了,我把代码,可以用来为其他人。干杯,再次感谢您提供的信息

代码语言:javascript
复制
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


}
}
}
}
票数 0
EN

Stack Overflow用户

发布于 2016-07-29 16:32:49

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

代码语言:javascript
复制
enum ActionSheetTag: Int {
        case ActionSheet1
        case ActionSheet2
}

。。。

代码语言:javascript
复制
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
            }
        }
    }

希望能有所帮助。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38652824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档