ios – 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标?

前端之家收集整理的这篇文章主要介绍了ios – 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我以编程方式创建了一个UIView并添加了一个UIButton作为它的子视图.
我希望UIViewController成为该按钮操作的目标.
我该怎么办?
如果它是由Interface Builder创建的,那么使用IBAction很容易.

解决方法

如果您以编程方式将按钮添加到UIView的子类,那么您可以通过以下两种方式之一执行此操作:

>您可以使按钮成为视图的属性,然后在实例化视图的viewController中,您可以按如下方式设置按钮的目标:

  1. [viewSubclass.buttonName addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

这会将按钮的目标设置为viewController.m中buttonTapped的方法
>您可以在子视图中创建协议,父视图控件将遵循该协议.在您的视图中,当您添加按钮时,将其设置为在视图中调用方法.然后从该视图中调用委托方法,以便viewController可以响应它:

在顶部您的视图子类.h创建协议:

  1. @protocol ButtonProtocolName
  2.  
  3. - (void)buttonWasPressed;
  4.  
  5. @end

为委托创建属性

  1. @property (nonatomic,assign) id <ButtonProtocolName> delegate;

在子类.m中设置按钮选择器:

  1. [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

在buttonTapped:方法调用委托方法

  1. - (void)buttonTapped:(id)sender {
  2. [self.delegate buttonWasPressed];
  3. }

在viewController.h中,您需要确保它符合协议:

  1. @interface someViewController : UIViewController <SomeButtonProtocolName>

在初始化子视图时,在viewController.m中,您必须设置委托:

  1. SomeView *view = ... // Init your view
  2. // Set the delegate
  3. view.delegate = self;

最后,将delegate方法buttonWasPressed添加到viewController.m:

  1. - (void)buttonWasPressed {
  2. // Put code here for button's intended action.
  3. }

更新以提供Swift示例

  1. // Simple delegate protocol.
  2. protocol SomeViewDelegate: class {
  3. // Method used to tell the delegate that the button was pressed in the subview.
  4. // You can add parameters here as you like.
  5. func buttonWasPressed()
  6. }
  7.  
  8. class SomeView: UIView {
  9. // Define the view's delegate.
  10. weak var delegate: SomeViewDelegate?
  11.  
  12. // Assuming you already have a button.
  13. var button: UIButton!
  14.  
  15. // Once your view & button has been initialized,configure the button's target.
  16. func configureButton() {
  17. // Set your target
  18. self.button.addTarget(self,action: #selector(someButtonPressed(_:)),for: .touchUpInside)
  19. }
  20.  
  21. @objc func someButtonPressed(_ sender: UIButton) {
  22. delegate?.buttonWasPressed()
  23. }
  24. }
  25.  
  26. // Conform to the delegate protocol
  27. class SomeViewController: UIViewController,SomeViewDelegate {
  28. var someView: SomeView!
  29.  
  30. func buttonWasPressed() {
  31. // UIViewController can handle SomeView's button press.
  32. }
  33. }

另外,这是一个使用闭包而不是委托的快速示例. (这也可以使用块在ObjC中实现.)

  1. // Use typeAlias to define closure
  2. typealias ButtonPressedHandler = () -> Void
  3.  
  4. class SomeView: UIView {
  5. // Define the view's delegate.
  6. var pressedHandler: ButtonPressedHandler?
  7.  
  8. // Assuming you already have a button.
  9. var button: UIButton!
  10.  
  11. // Once your view & button has been initialized,for: .touchUpInside)
  12. }
  13.  
  14. @objc func someButtonPressed(_ sender: UIButton) {
  15. pressedHandler?()
  16. }
  17. }
  18.  
  19. class SomeViewController: UIViewController {
  20. var someView: SomeView!
  21.  
  22. // Set the closure in the ViewController
  23. func configureButtonHandling() {
  24. someView.pressedHandler = {
  25. // UIViewController can handle SomeView's button press.
  26. }
  27. }
  28. }

猜你在找的iOS相关文章