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
.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
You can add target and action to button.
Target is controller so If you want to handle touch event in current controller use self. Other wise you should have a pointer/reference to the controller obj, and use that reference instead of the self.
onTapButton
is selector which will be called when user tap on the button.onTapButton
do not taking any parameter, If you want to use with parameter useonTapButton:
NOTE: Better way is to handle this is to use delegation pattern, have target in self what ever class that is, and After that call delegate, and controller should implement that delegate. Keeping direct reference to the controller is not good practice.
If you are adding the button programmatically to a subclass of UIView, then you can do it one of two ways:
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:
This will set the button's target to a method of buttonTapped: in the viewController.m
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:
In the top your view subclass .h create the protocol:
Create a property for the delegate:
In the subclass .m set your button selector:
In the buttonTapped: method call the delegate method:
In your viewController.h you'll need to make sure it conforms to the protocol:
In your viewController.m when you init your subview, you'll have to set the delegate:
Finally, add the delegate method buttonWasPressed to the viewController.m:
Updated to provide Swift example
Additionally, here is a quick example using a closure instead of a delegate. (This can approach also be implemented in ObjC using blocks.)
We can achieve this without delegates as well.
In your
CustomUIView.m
class, implement the following method:-In your
viewcontroller.m
file, write the below code to add custom uiview and set target of button:-Now, if you are using UINavigationController your buttonPressed function will be
If you are not using navigation Controller than
For Swift 4
In ViewController
Hope this helps. Happy coding :]