在 Swift 3 和 Xcode 中使用 stackview 按钮打开故事板视图 [英] Open a storyboard view with a stackview button in Swift 3 and Xcode

查看:28
本文介绍了在 Swift 3 和 Xcode 中使用 stackview 按钮打开故事板视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have three views in my storyboard: MainViewController, EditViewController and PatchViewController.

在我的主视图中,我添加了一个水平堆栈视图对象,它有自己的类(class FieldController: UIStackView),我在其中以编程方式添加了三个按钮.根据一些动态值,点击这些按钮之一应该打开编辑或补丁视图,它们也应该可以访问与点击的按钮相对应的 id.

In my Main view I've added a Horizontal Stack View-object which has its own class (class FieldController: UIStackView) where I add three buttons programatically. Depending on some dynamic values, clicking on one of these buttons should open either the Edit or Patch-view which also should have access to an id that corresponds to what button clicked.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

首先,您需要在故事板中创建从主控制器到附加控制器的转场,并为每个转场设置不同的标识符.在我的示例中,我使用 EditSegueIdentifier 和 PatchSegueIdentifier

First, you need to create segues in your storyboard from main controller to your additional controllers and set different identifier for each segue. In my example I'm using EditSegueIdentifier and PatchSegueIdentifier

然后在您的主控制器中:

Then in your main controller:

//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
    }

}

如果您使用以编程方式创建的按钮,您可以在它们的选择器方法中做同样的事情:

If you are using programatically created buttons, you can make same thing inside their selector method:

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

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

Then we need to prepare values to send to additional controllers

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 中

Then in your 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        
         }
  }

然后使用委托添加视图控制器的扩展

Then add an extension of you view controller with the delegate

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

这篇关于在 Swift 3 和 Xcode 中使用 stackview 按钮打开故事板视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆