Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create event for UIButton in ViewController?

I add a UIButton programmatically, and I want to call a method when pressing on it, I'm not sure how to design such a pattern:

  1. where should I put event handler, or action method? in view controller or view itself.

  2. If I put the method in view controller, how can I manipulate the SubViews of the View in the method? should I expose all of SubViews (UIButton, etc.) to controller by putting them in view's header file as properties? Actually this question shall be asked in this way: How can I implement by code that SubViews in a view are associated to an IBOutlet properties by Interface Builder...

like image 225
Alan Hoo Avatar asked Dec 13 '15 15:12

Alan Hoo


People also ask

How can I check if UIButton is pressed?

If you want to check If UIButton is Pressed or not, You should handle TouchDown Handler and change button's state to pressed in touchDown hadnler. You can track ToucUpInside method to Change state of button to Not-pressed again. Thank you so much. By using the addTarget syntax I solved my problem!

What is a UIButton?

A control that executes your custom code in response to user interactions.

How do I give an action to a button in IOS Swift?

Set Button Action When the button is tapped we can make it call a function. To add an action call to our button we will need to use the addTarget() function.


1 Answers

Check this link for the basics of iOS view hierarchy: Getting started with iOS Views and understand the following diagram (credits: techrepublic.com):

Credits: http://www.techrepublic.com/

Programmatically:

// Create your button wherever you wish (below is example button)
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(0.0, 0.0, 320, 450);
[myButton setTitle:@"Yay" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(didTouchUp:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];

// This method will be called when touch up is made on the button
- (void)didTouchUp:(UIButton *)sender {
    NSLog(@"Button Pressed!");
}

Explanation:

[myButton addTarget:self action:@selector(didTouchUp:) forControlEvents:UIControlEventTouchUpInside];
  • myButton - your button view
  • addTarget - the target in which the method exist in
  • action - the selector you want to call
  • forControlEvents - control event the user will do to trigger this action

Using Storyboards:

enter image description here

  • (A) Make sure the storyboard is corresponding to the correct class
  • (B) Drag UIButton from the object library into the storyboard

Then add to the ViewController.h file:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UIButton *myButton;

- (IBAction)didTouchUp:(id)sender;

@end

Then add to the ViewController.m file:

- (IBAction)didTouchUp:(id)sender {
    NSLog(@"Button Pressed!");
}

Lastly, create the connection between the UIButton and the IBAction:

enter image description here

  • (A) Right click on the button view
  • (B) Drag touch up action to the button view on the storyboard
  • (C) Press on didTouchUp:

This is the very basic flow to do such a step... you might want to expend your skills by reading the following:

  • Storyboard vs IB vs Code
  • Stanford University Developing iOS
like image 79
Roy K Avatar answered Oct 28 '22 08:10

Roy K