如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标? [英] How to add UIViewController as target of UIButton action created in programmatically created UIView?
问题描述
我以编程方式创建了一个 UIView
并添加了一个 UIButton
作为它的子视图。
I希望 UIViewController
成为该按钮操作的目标。
我该怎么做?
如果是由Interface Builder创建,然后使用 IBAction
很容易。
I created a UIView
programmatically and added a UIButton
as it's subview.
I want a UIViewController
to be the target of that button action.
How would I do that?
If it was created by Interface Builder then it was easy by using IBAction
.
推荐答案
如果您以编程方式将按钮添加到UIView的子类中,那么您可以通过以下两种方式之一完成:
If you are adding the button programmatically to a subclass of UIView, then you can do it one of two ways:
-
您可以使按钮成为视图的属性,然后在实例化视图的viewController中,您可以按如下方式设置按钮的目标:
You can make the button a property of the view, and then in the viewController that instantiates the view you can set the target of the button as follows:
[viewSubclass.buttonName addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
这会将按钮的目标设置为buttonTapped的方法:在viewController.m中
This will set the button's target to a method of buttonTapped: in the viewController.m
您可以在子视图中创建协议,父视图控件将遵循该协议。在您的视图中,当您添加按钮时,将其设置为在视图中调用方法。然后从该视图调用委托方法,以便viewController可以响应它:
You can create a protocol in your subview, which the parent viewController will conform to. In your view, when you add your button set it to call a method in your view. Then call the delegate method from that view so that your viewController can respond to it:
在视图子类的顶部.h创建协议:
In the top your view subclass .h create the protocol:
@protocol ButtonProtocolName
- (void)buttonWasPressed;
@end
为代表创建一个属性:
@property (nonatomic, assign) id <ButtonProtocolName> delegate;
在子类.m中设置按钮选择器:
In the subclass .m set your button selector:
[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
在buttonTapped:方法中调用委托方法:
In the buttonTapped: method call the delegate method:
- (void)buttonTapped:(id)sender {
[self.delegate buttonWasPressed];
}
在viewController.h中,你需要确保它符合协议:
In your viewController.h you'll need to make sure it conforms to the protocol:
@interface someViewController : UIViewController <SomeButtonProtocolName>
在初始化子视图时,在viewController.m中,您必须设置委托:
In your viewController.m when you init your subview, you'll have to set the delegate:
SomeView *view = ... // Init your view
// Set the delegate
view.delegate = self;
最后,将委托方法buttonWasPressed添加到viewController.m:
Finally, add the delegate method buttonWasPressed to the 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中实现。)
Additionally, here is a quick example using a closure instead of a delegate. (This can approach also be implemented in ObjC using blocks.)
// 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, configure the button's target.
func configureButton() {
// Set your target
self.button.addTarget(self, action: #selector(someButtonPressed(_:)), 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.
}
}
}
这篇关于如何添加UIViewController作为在以编程方式创建的UIView中创建的UIButton操作的目标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!