【问题标题】:Open new view controller page according to value chosen from UIPickerView (Swift 4)根据从 UIPickerView (Swift 4) 中选择的值打开新的视图控制器页面
【发布时间】:2019-02-01 04:40:29
【问题描述】:

我想在我的应用程序中添加一个功能,用户必须从 UIPickerView 中选择一个类别,单击按钮后,程序将打开从 UIPickerView 中的类别中选择的视图。例如,如果 UIPickerView 上选择的选项之一是“关于页面”,我希望有一个程序可以在单击按钮时打开“关于页面”。

这就是 UI 的样子。

但不是状态 - 我会有类别,我不确定如何在 UIPickerView 中设置值并打开特定视图。

【问题讨论】:

    标签: swift button uiviewcontroller uipickerview


    【解决方案1】:

    实现UIPickerViewDelegate方法并给每个VC一个stroryboard Identifier

    var lastId:String?
    
    var ids = ["secondVC","thirdVC"] // picker data source and identifier names 
    
    func pickerView(_ pickerView: UIPickerView, 
            didSelectRow row: Int, 
             inComponent component: Int) {
       lastId = ids[row]
    }
    
    @IBAction func goClicked(_ sender:UIButton) {
        if let id = lastId {
          let vc = storyboard.instantiateViewController(withIdentifier:id)!
          self.present(vc, animated: true, completion: nil) // or push
        }
        else {
          // select a vc alert
        }
    }
    

    【讨论】:

    • 另外,ID 可以是与 performSegue(withIdentifier:sender:) 一起使用的 segue 标识符。
    • @vacawama yes 是 op 可以用来代替 push/present 的另一种选择
    • @Sh_Khan - 非常感谢你的帮助 - 我想我理解代码 - 但我对 UIPickerView 的想法很陌生 - 你能帮我实现 UIPickerViewDelegate 方法吗?请问?
    • 看看答案 didSelectRow
    【解决方案2】:

    如果您想以编程方式创建选择器视图(我最推荐这种方式),此解决方案应该适合您:

    class MyViewController : UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
        
        let pickerView = UIPickerView()
        let titles = ["view1", "view2"]
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = .white
            
            pickerView.delegate = self
            pickerView.dataSource = self
            
            view.addSubview(pickerView)
        }
        
        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return 2
        }
        
        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return titles[row]
        }
        
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }
        
        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            if row == 0 {
                let view1 = viewController1()
                self.present(view1, animated: true, completion: nil)
            }
            if row == 1 {
                let view2 = viewController2()
                self.present(view2, animated: true, completion: nil)
            }
        }
        
    }
    
    class viewController1 : UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = UIColor.white
        }
    }
    
    class viewController2 : UIViewController {
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            view.backgroundColor = UIColor.white
        }
    }

    希望这对你也有用;)

    【讨论】:

    • 非常感谢 - 请您解释一下为什么您建议我以编程方式而不是使用情节提要标识符来添加它?
    • 因为您可以更好地了解代码中发生的事情,并且您可以使用代码比使用情节提要更快地更改内容。如果您想获得 AppDeveloper 的工作,也更推荐使用它,因为 Instagram 和 Facebook 等大公司都在编写代码而不是使用故事板,因为与每次更改内容相比,更多人编写代码也更容易。故事板。希望这能很好地解释它?
    猜你喜欢
    • 2018-11-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多