swift - 根据从 UIPickerView (Swift 4) 选择的值打开新的 View Controller 页面

标签 swift button uiviewcontroller uipickerview

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

这就是用户界面的样子。 enter image description here

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

最佳答案

实现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
    }
}

关于swift - 根据从 UIPickerView (Swift 4) 选择的值打开新的 View Controller 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52025808/

相关文章:

ios - 小计算器

c++ - Xcode 8、Swift 和 C++

ios - 有没有真正的方法来更改 UIDatePicker - Swift 3 上选定日期的文本颜色?

javascript - 新窗口中的链接是可选的

ios - 如何将数据从先前的 segue 传递给 subview Controller ?

ios - 适用于 iPad 的 NumberPad?

asp.net - 圆角按钮 ASP.NET

ios - 尝试显示另一个 View Controller 时的 Sigabrt

ios - 在另一个 UIViewController 中嵌入 UIViewController View

ios - UINavigationController barstyle 属性改变布局