UIButton Tutorial

Matthew Campbell, August 16, 2011

iPhone apps use buttons to let users signal intentions to our iOS app. UIKit has a special class called UIButton, a subclass of UIControl, that gives us an easy way to add buttons to our app.

Let's talk about how you would add a button to your app.


Adding UIButtons To iOS Apps

First, let's assume that you already have a view based application set up. You can do this easily by using XCode's Single View Application project template.

Step One

- Locate the view controller's header file. This will be called something like ViewController.h.

- Add this forward declaration for a pressButton: method (this is what will execute when a user touches a button):

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController

-(void)pressButton:(id)sender;

@end

Step Two 

Locate the view controller’s implementation file.  This will be called something like ViewController.m. Add class level variables for the label and button we will be using:

#import "ViewController.h"
@implementation ViewController
UILabel *label;UIButton *nextButton;

-(void)viewDidLoad{
     [super viewDidLoad];
}

@end

Next, add the implementation for the pressButton: method:

#import "ViewController.h"
@implementation ViewController
UILabel *label;UIButton *nextButton;

-(void) pressButton:(id)sender{ if([label.text isEqual:@"Button Pressed!"]) label.text = @"Press Button Again"; else label.text = @"Button Pressed!"; }

- (void)viewDidLoad{
     [super viewDidLoad];
}

@end

Step Three

In your view controller implementation file locate the method called -(void)viewDidLoad{}.  Add the code necessary here to add a label to your view.

#import "ViewController.h"
@implementation ViewController
UILabel *label;UIButton *nextButton;

-(void) pressButton:(id)sender{
     if([label.text isEqual:@"Button Pressed!"])
          label.text = @"Press Button Again";
     else
          label.text = @"Button Pressed!";
}

-(void)viewDidLoad{
     [super viewDidLoad];
     label = [[UILabel alloc] init]; label.frame = CGRectMake(10, 10, 300, 40); label.textAlignment = UITextAlignmentCenter; label.text = @"Press Button"; [self.view addSubview:label]; 
}

@end

Step Four

Finally, add the code to create a button and assign the pressButton: method to it:

#import "ViewController.h"
@implementation ViewController
UILabel *label;UIButton *nextButton;

-(void) pressButton:(id)sender{
     if([label.text isEqual:@"Button Pressed!"])
          label.text = @"Press Button Again";
     else
          label.text = @"Button Pressed!";
}

-(void)viewDidLoad{
     [super viewDidLoad];
     label = [[UILabel alloc] init];
     label.frame = CGRectMake(10, 10, 300, 40);
     label.textAlignment = UITextAlignmentCenter;
     label.text = @"Press Button";
     [self.view addSubview:label];
     UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; nextButton.frame = CGRectMake(110, 200, 100, 50); [nextButton addTarget:self action:@selector(pressButton:) forControlEvents:UIControlEventTouchUpInside]; [nextButton setTitle:@"Press" forState:UIControlStateNormal]; [self.view addSubview:nextButton];
}
@end

And That's It - UIButton Created!