pass custom parameter to uibutton #selector swift

2019-07-25 10:03发布

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

class 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")
  }
}

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

but this throws an error unrecognized selector sent to instance 0x7b25e010'

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

1条回答
手持菜刀,她持情操
2楼-- · 2019-07-25 10:16

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

  1. Nothing
  2. The object itself (in this case the button)

These scenarios would look like this:

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

or

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

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.

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.

Hope this helped.

查看更多
登录 后发表回答