I have subclassed a UIView. I am adding it as a subview of a UIViewController view. I have a button in the UIView. What I want is to handle the button click inside my UIViewController, where I will remove the UIView from superview and push another UIViewController. How can I do this.
You must attach the target with your UIButton. addTarget method is used for getting the event when you click on button and defined in UIControl class which is direct super class of UIButton
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
Code to receive the event for Button touch.
[myButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Implementation for the action.
-(void) buttonClicked:(id) sender {
UIButton* myButton = (UIButton*)sender;
//Put your code here
}
Related
I have a UIViewController which I want to display a UIView that renders as a menu. This menu will have several buttons on it. I wanted to reuse this menu a few different places in my app so I figured I would create a class called ViewFactory that has a method that returns a UIView with these buttons.
On my ViewController I call this method and get the returned UIView and add it as a subview.
This works just fine. I can see the view and all its buttons, however, the buttons do not respond to any touch events. Not sure why this is the case and curious to know what I am doing wrong.
Here is my code for the ViewFactoryClass:
- (UIView *) addCloseRow
{
// UIView container for everything else.
UIView *navRow = [[UIView alloc] initWithFrame:CGRectMake(0,225,350,45)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.userInteractionEnabled = YES;
[navRow addSubview:button];
[button addTarget:self action:#selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown];
navRow.userInteractionEnabled = YES;
return navRow;
}
In my main NavigationController class here is how I am calling and getting the UIView:
ViewFactory *factory = [[ViewFactory alloc] init];
[self.navigationController.navigationBar addSubview:[factory MainNavigationUIView]];
Again, the UIView shows up but the buttons never respond to anything.
You added the button with target and selector for ViewFactoryClass
And now you are creating instance and trying to call an action from ViewFactory class.
You can change the method to something like this:
- (UIView *) addCloseRow : (id)object {
...
[button addTarget:[object class] action:#selector(closeButtonTouchDownEvent) forControlEvents: UIControlEventTouchDown];
...
}
I have subclass of UIButton:
#interface MyButton : UIButton
- (IBAction) buttonPressed:(id)sender;
#end
#implementation MyButton
- (void)awakeFromNib
{
NSLog(#"awake from nib");
}
- (IBAction) buttonPressed:(id)sender
{
NSLog(#"button pressed");
}
#end
I add a button in my parent xib, set it's class to MyButton, and connected it's action to First Responder's method buttonPressed.
When I start an application and load my parent xib with my button inside, than awakeFromNib from MyButton class is called. But when I press the button, nothing happens. I was expecting that my method buttonPressed from MyButton class will be called.
I supposed, that my button's view is the first responder in responder chain, but apparently I do something wrong.
Could you please suggest something?
If you want that your button always call the method declared add the code above to the awakeFromNib method
[self addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
and later when you added it to a view controller do not assign it a new action, this way it will always call your method.
Depending on what you are doing with the button - there is no real need to make a subclass.
Here is some code how to make a simple button that triggers a method:
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[myButton setFrame:CGRectMake(10, 10, 100, 30)];
[myButton setTitle:#"Title" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myMethod:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
the add target bit tells the button to trigger the given selector when the user touches up inside the button (taps it). The target is the object that the selector is in.
Hope this helps, you should not need to subclass UIButton unless you have custom layouts or specific custom functionality for it.
Since you want to handle the action from the button code itself, you may have to override this method - (void)sendActionsForControlEvents:(UIControlEvents)controlEvents and handle your event as if it was sent to your button;
I have an if statement that is bringing up a SVProgressHUD that lets the user know there is an error. I would like to create a touch event programmatically but I am not sure how to define my view when I am using a tableview which was set up in interface builder
this is the code I would like to use
[myView addTarget:self action:#selector(myAction:)forControlEvents:UIControlEventTouchUpInside];
but I am not sure how to create myView when I am using a tableview. What is the best way to handle this?
You can add a UIButton with transparent color and add it to your view.
Then you'll be able to use the following method:
[myButton addTarget:self action:#selector(myAction:)forControlEvents:UIControlEventTouchUpInside];
Add tapGestureRecognizer to the view with target and event to handle the tap event such that when a user click or touches the view it triggers the selector.
UIGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(mySelector:)]
[myView addGestureRecognizer: recognizer];
Then you could add the selector which simply takes the recognizer as the sender;
-(void)mySelector:(UIGestureRecognizer*)gr{
// handling event
}
For example, I have two same buttons in all view controllers. And reaction of these buttons are the same in all view controllers. How I can make this functional in common class and use this class or object in every view controllers?
When you add the couple target/action to the button, you can set the target to a specific class:
[button1 addTarget:myOtherClassInstance action:#selector(doSomethingAction:) forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:myOtherClassInstance action:#selector(doSomethingAction2:) forControlEvents:UIControlEventTouchUpInside];
and in the myOtherClass.m
- (void)doSomethingAction:(id)sender {
//Do something and maybe check the sender
}
- (void)doSomethingAction2:(id)sender {
//Do some other thing and maybe check the sender
}
you can put the action method in the delegate class , and link the button tap event to this method like this
[yourButton addTarget:yourDelegate action:#selector(yourmethod:) forControlEvents:UIControlEventTouchUpInside];
In my application, i am using UIViewController and dynamically created UIView.In that view dynamically created subviews(UIButton and PickerViewController)..I want to display selected picker view value in a label control in UIViewController... How to add action to this button to move back to UIViewController...
You can either pass the reference of the UIViewCOntroller to the subview or you can create delegate methods for the subview.
Method 1 :
//below code is written in your UIViewController
/*You should write the implementation of initWithFrame:viewController: in UIVIEW
CustomUIView *youSubView = [[CustomUIView alloc]initWithFrame : yourFrame viewController:self];
//below code is in CustomUIView
- (id) initWithFrame :(CGRect)frame viewController:(id)vc
{
....
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:vc action:methodinVc forControlEvents:UIControlEventTouchUpInside];
}
Method 2 :
Make protocol and delegate in UIView and make the UIViewController to respond to the delegate calls