将自定义参数传递给 uibutton #selector swift 3 [英] pass custom parameter to uibutton #selector swift 3

查看:40
本文介绍了将自定义参数传递给 uibutton #selector swift 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,我将 uistackviews 从一个类传递到另一个类.我希望在同一个 stackview 中创建控件.因此,我在所有渲染函数参数中传递视图.我还希望通过 uibutton

I have a 2 classes where I am passing uistackviews from one class to other. I want the controls to be created in same stackview. Hence I am passing the view in all the render function parameters. I also want that view to be passed with action #selector of uibutton

第 1 类:

class ViewController: UIViewController {    
  func createbutton(parentview: UIStackView) {
    let buttn = UIButton()
    buttn.backgroundColor = .gray
    buttn.setTitle("testttt", for: .normal)
    buttn.frame.size.height = 30
    buttn.frame.size.width = 40
    buttn.addTarget(self, action: #selector(anotherbutton(parentview:)), for: .touchUpInside)
    parentview.addArrangedSubview(buttn)
  } 

  func anotherbutton(parentview: UIStackView) {
    //another button here
  }    

  func loadpage() {
    print("loadpage")
  }
}

第 2 类:

class plugin : UIViewController {       

let vw = ViewController()

override func viewDidLoad() {
    super.viewDidLoad()
    let parentview = getparentnode()
    vw.createbutton(parentview: parentview)
}

func getparentnode() -> UIStackView {
    let parentnode =  UIStackView()
    parentnode.axis  = UILayoutConstraintAxis.vertical
    parentnode.distribution  = UIStackViewDistribution.equalSpacing
    parentnode.alignment = UIStackViewAlignment.center
    parentnode.spacing   = 16.0
    parentnode.tag = 50
    parentnode.translatesAutoresizingMaskIntoConstraints = false;

    self.view.addSubview(parentnode)
    //Constraints
    parentnode.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive = true
    parentnode.centerYAnchor.constraint(equalTo: self.view.centerYAnchor).isActive = true
    return parentnode
}
}

但这会引发错误 unrecognized selector sent to instance 0x7b25e010'

如何在动作选择器参数中传递 UIView?感谢您的帮助

How to pass the UIView in action selector parameter ? Thank you for any help

推荐答案

你不能.您可以通过选择器传递的唯一内容是:

You can't. The only things that you can pass through a selector is:

  1. 没什么
  2. 对象本身(在本例中为按钮)

这些场景看起来像这样:

These scenarios would look like this:

button.addTarget(self, action: #selector(myFunc), ...) //no parameters

button.addTarget(self, action: #selector(myFunc(_:)) //passes itself (the button)

如果您想将视图的值传递给另一个 ViewController,我建议使用 prepareForSegue 方法.这就是你应该如何将数据从 ViewController 传递到 ViewController.

If you want to pass the value of a view to another ViewController I recommend using the prepareForSegue method. That is how you are supposed to pass data from ViewController to ViewController.

就您的其余代码而言,我相信您通过在另一个类中创建类的实例来打破 MVC 设计模式(这一行:let vw = ViewController()).首先,如果您的 ViewController 与您设备上运行的不同,这将创建一个全新的实例.其次,这是不好的做法.您应该允许每个 viewController 管理自己,并且不会受到其他 viewController 的外部干扰.使用 prepareForSegue 是有效使用 MVC 设计模式的示例.

In terms of the rest of your code, I believe you are breaking the MVC design pattern by creating an instance of your class in another class (this line: let vw = ViewController()). First of all, this will create an entirely new instance if your ViewController, which isn't the same as the one running on your device. Second of all, this is bad practice. You should be allowing each viewController to manage itself and not have outwards interference from other viewControllers. Using prepareForSegue is an example of using the MVC design pattern effectively.

希望这有帮助.

这篇关于将自定义参数传递给 uibutton #selector swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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