使用Swift 3和Xcode中的stackview按钮打开故事板视图

时间:2017-08-13 10:14:28

标签: swift xcode

我的故事板中有三个视图:MainViewController,EditViewController和PatchViewController。

在我的主视图中,我添加了一个水平堆栈视图对象,它有自己的类(类FieldController:UIStackView ),我以编程方式添加三个按钮。 根据某些动态值,单击其中一个按钮应打开编辑或修补程序视图,该视图也应该可以访问与单击的按钮对应的ID。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:1)

首先,您需要在故事板中创建从主控制器到其他控制器的segue,并为每个segue设置不同的标识符。在我的例子中,我正在使用EditSegueIdentifier和PatchSegueIdentifier

然后在你的主控制器中:

//connect IBActions to each button, or your buttons may have one action to connect to, but have also different tags, like below:
@IBAction func buttonAction(_ sender: UIButton) {
    //You can send an ID as Int for example, to catch them in prepareForSegue method. 
    //You can send any values or objects as sender, 
    //also it can be a tag of clicked button, whatever you want.
    switch sender.tag{
        case 1:
          performSegue(withIdentifier: "EditSegueIdentifier", sender: YourIDForEDIT)
        case 2:
          performSegue(withIdentifier: "PatchSegueIdentifier", sender: YourIDForPATCH)
        default: break
    }

}

如果您使用以编程方式创建的按钮,则可以在其选择器方法中使用相同的内容:

func setTargets(){
    let selector = #selector(buttonAction(_:))
    yourFirstButton.tag = 1
    yourFirstButton.addTarget(self, action: selector, for: .touchUpInside)

   yourSecondButton.tag = 2
   yourSecondButton.addTarget(self, action: selector, for: .touchUpInside)
}

func buttonAction(_ sender: UIButton){
     switch sender.tag{
            case 1:
              performSegue(withIdentifier: "EditSegueIdentifier", sender: YourIDForEDIT)
            case 2:
              performSegue(withIdentifier: "PatchSegueIdentifier", sender: YourIDForPATCH)
            default: break
        }
}

然后我们需要准备值以发送给其他控制器

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        //check segue identifier for sending values to destination controller
        if segue.identifier == "EditSegueIdentifier", let editID = sender as? Int {

            let destination = segue.destination as? EditViewController
            destination?.id = editID        
        }

        if segue.identifier == "PatchSegueIdentifier", let patchID = sender as? Int {

            let destination = segue.destination as? PatchViewController
            destination?.id = patchID        
        }
}

============== EDIT ==================

协议

protocol YourStackViewDelegate {
    func performButtonAction(with index: Int) //or you can send anything else (e.g. ID, objects)
}

然后在你的StackViewClass

class YourStackViewClass {
   //make optional var for delegate instance
    var delegate: YourStackViewDelegate?

   func setTargets(){
        let selector = #selector(buttonAction(_:))
        yourFirstButton.tag = 1
        yourFirstButton.addTarget(self, action: selector, for: .touchUpInside)

       yourSecondButton.tag = 2
       yourSecondButton.addTarget(self, action: selector, for: .touchUpInside)
    }

    func buttonAction(_ sender: UIButton){
         //call the method from the protocol
         delegate?.performButtonAction(with: sender.tag)
    }
}

在视图控制器中

override func viewDidLoad() {
   super.viewDidLoad()

   yourStackView.delegate = self
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            //check segue identifier for sending values to destination controller
         if segue.identifier == "EditSegueIdentifier", let editID = sender as? Int {

             let destination = segue.destination as? EditViewController
             destination?.id = editID        
         }

         if segue.identifier == "PatchSegueIdentifier", let patchID = sender as? Int {

             let destination = segue.destination as? PatchViewController
             destination?.id = patchID        
         }
  }

然后使用委托

添加视图控制器的扩展名
extension YourViewController: YourStackViewDelegate {
    func performButtonAction(with index: Int) {
         switch index{
                case 1:
                  performSegue(withIdentifier: "EditSegueIdentifier", sender: YourIDForEDIT)
                case 2:
                  performSegue(withIdentifier: "PatchSegueIdentifier", sender: YourIDForPATCH)
                default: break
            }
    }
}