简体   繁体   English

如何在Swift4中获取另一个类的枚举内容?

[英]How to get enum content of another class in Swift4?

I want navigate to another view controller that have a picker view, but this is generic, the content pickview must be selected according a type parameter passed before navigate. 我想导航到另一个具有选择器视图的视图控制器,但这是通用的,必须根据导航之前传递的类型参数选择内容选择视图。 This parameter is a variable named tipo 此参数是一个名为tipo的变量

So in my MainViewController I have a func to navigate: 所以在我的MainViewController中,我有一个可以浏览的函数:

@objc func handlerSelectOpTipo(_ sender: UILabel) {
        let vcSelectTipo = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerSelect") as! ViewControllerSelect
       //  vcSelectTipo.tipo = .entrega // <- Example that I want.
        vcSelectTipo.tipo = 0 // <- ENUM TYPE HERE
        self.present(vcSelectTipo,animated: true,completion: nil)
    }

Picker`s ViewController Picker的ViewController

class ViewControllerSelect : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var pickerView: UIPickerView!
    @IBOutlet weak var btOk: UIButton!
    @IBOutlet weak var lblTitulo: UILabel!
    var tipo : Int?
    var conteudo : [String] = []

    override func viewDidLoad() {
        pickerView.setValue(UIColor.white, forKey: "textColor")
        pickerView.dataSource = self
        pickerView.delegate = self
        btOk.addTarget(self, action: #selector(handlerBtOk), for: .touchUpInside)
    }

    @objc func handlerBtOk() {

    }


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

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

    enum tipoConteudo: Int {
        case tipo
        case entrega
        case priceList
        case campanha
    }
}

I can't access enum values of MainViewController for set var tipo : Int? 我无法访问Set var tipo : Int?的MainViewController枚举值var tipo : Int?

I wants some like: 我想要一些像:

vcSelectTipo.tipo = .entrega
vcSelectTipo.tipo = .priceList

but the autocomplete not show tipoConteudo enum type. 但是自动完成功能不会显示tipoConteudo枚举类型。

All you need to do is change 您需要做的就是改变

var tipo: Int? 

to

var tipo: tipoConteudo?

You just had the incorrect type for tipo. 您输入的Tipo类型不正确。

You've declared the tipo variable as an Int instead of your enum type. 您已将tipo变量声明为Int而不是枚举类型。

Change: 更改:

var tipo: Int?

to: 至:

var tipo: TipoConteudo?

And type names should start with uppercase letters so tipoConteudo should be TipoConteudo . 并且类型名称应以大写字母开头,因此tipoConteudo应该为TipoConteudo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM