Example code of UIButton

  1. Create a UIButton called ‘pressHere’ that has nonatomic and weak property.

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

  • IBOutlet makes Interface Builder recognize the outlet
  • weak is used because in most situations the owner of the outlet isn’t the same as the owner of the view. For example, a view controller doesn’t own someLabel – the view controller’s view does.

2.  Set the background color for that button

pressHere.backgroundColor = [UIColor clearColor];

3. Set a title for the button

[pressHere setTitle:@”Click” forState:UIControlStateNormal];

4. Add an action for the button

[pressHere addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

Notice that “butClick” is a -(void) that has been defined somewhere else in the .m file.


Read more:

How to add an action for a button in StoryBoard?