Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you are adding the button programmatically to a subclass of UIView, then you can do it one of two ways:</p> <ol> <li><p>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:</p> <pre><code>[viewSubclass.buttonName addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; </code></pre> <p>This will set the button's target to a method of buttonTapped: in the viewController.m</p></li> <li><p>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:</p></li> </ol> <p>In the top your view subclass .h create the protocol:</p> <pre><code>@protocol ButtonProtocolName - (void)buttonWasPressed; @end </code></pre> <p>Create a property for the delegate:</p> <pre><code>@property (nonatomic, assign) id &lt;ButtonProtocolName&gt; delegate; </code></pre> <p>In the subclass .m set your button selector:</p> <pre><code>[button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside]; </code></pre> <p>In the buttonTapped: method call the delegate method:</p> <pre><code>- (void)buttonTapped:(id)sender { [self.delegate buttonWasPressed]; } </code></pre> <p>In your viewController.h you'll need to make sure it conforms to the protocol:</p> <pre><code>@interface someViewController : UIViewController &lt;SomeButtonProtocolName&gt; </code></pre> <p>In your viewController.m when you init your subview, you'll have to set the delegate:</p> <pre><code>SomeView *view = ... // Init your view // Set the delegate view.delegate = self; </code></pre> <p>Finally, add the delegate method buttonWasPressed to the viewController.m:</p> <pre><code>- (void)buttonWasPressed { // Put code here for button's intended action. } </code></pre> <p><strong>Updated to provide Swift example</strong></p> <pre><code>// 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 &amp; 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. } } </code></pre> <p>Additionally, here is a quick example using a closure instead of a delegate. (This can approach also be implemented in ObjC using blocks.)</p> <pre><code>// Use typeAlias to define closure typealias ButtonPressedHandler = () -&gt; Void class SomeView: UIView { // Define the view's delegate. var pressedHandler: ButtonPressedHandler? // Assuming you already have a button. var button: UIButton! // Once your view &amp; 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. } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload