HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我以编程方式创建了一个UIView并添加一个UIButton作为它的子视图.
我希望UIViewController成为该按钮操作的目标.
该怎么办
如果它是由Interface Builder创建的,那么使用IBACtion很容易.

解决方法

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

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

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

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

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

@protocol ButtonProtocolName

- (void)buttonWasPressed;

@end

为委托创建属性

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

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

[button addTarget:self action:@SELEctor(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];

在buttonTapped:方法调用委托方法

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

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

@interface someViewController : UIViewController <SomeButtonProtocolName>

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

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

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

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

更新以提供Swift示例

// Simple delegate protocol.
protocol SomeViewDelegate: class {
  // Method used to tell the delegate that the button was pressed in the subview.
  // You can add parameters here as you like.
  func buttonWasPressed()
}

class SomeView: UIView {
  // Define the view's delegate.
  weak var delegate: SomeViewDelegate?

  // Assuming you already have a button.
  var button: UIButton!

  // Once your view & button has been initialized,configure the button's target.
  func configureButton() {
    // Set your target
    self.button.addTarget(self,action: #SELEctor(someButtonPressed(_:)),for: .touchUpInsidE)
  }

  @objc func someButtonPressed(_ sender: UIButton) {
    delegate?.buttonWasPressed()
  }
}

// Conform to the delegate protocol
class SomeViewController: UIViewController,SomeViewDelegate {
  var someView: SomeView!

  func buttonWasPressed() {
    // UIViewController can handle SomeView's button press.
  }
}

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

// Use typeAlias to define closure
typealias ButtonPressedHandler = () -> Void

class SomeView: UIView {
  // Define the view's delegate.
  var pressedHandler: ButtonPressedHandler?

  // Assuming you already have a button.
  var button: UIButton!

  // Once your view & button has been initialized,for: .touchUpInsidE)
  }

  @objc func someButtonPressed(_ sender: UIButton) {
    pressedHandler?()
  }
}

class SomeViewController: UIViewController {
  var someView: SomeView!

  // Set the closure in the ViewController
  func configureButtonHandling() {
    someView.pressedHandler = {
      // UIViewController can handle SomeView's button press.
    }
  }
}

大佬总结

以上是大佬教程为你收集整理的ios – 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标?全部内容,希望文章能够帮你解决ios – 如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。