Creating Basic Buttons

// Implement loadView to create a view hierarchy programmatically,
// without using a nib.
- (void)loadView {
       
  //allocate the view
  self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
       
  //set the view’s background color
  self.view.backgroundColor = [UIColor whiteColor];
 
  //create the button
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       
  //set the position of the button
  button.frame = CGRectMake(100, 170, 100, 30);
 
  //set the button’s title
  [button setTitle:@“Click Me!” forState:UIControlStateNormal];
 
  //add the button to the view
  [self.view addSubview:button];
}

The first thing we need to do is create a button. The UIButton object has a class method called buttonWithType that automatically creates buttons with various types. We just want a regular old button so I passed in UIButtonTypeRoundedRect. Next up, we need to position and size the button, so I set the frame property to my specified bounds. A button’s not much use without text, and setTitle is used to set it. You can specify a different title for each state, and if a title is not set for a specific state, it will use whatever was set for UIControlStateNormal. That’s why it’s the only state we’re setting a title for. All that’s left now is to add it as a subview to the UIViewController’s view object. If you were to compile and run the code you’d see a button in the center of the screen.

iPhone Button Screenshot

A button doesn’t do us a lot of good unless we know when it’s been pressed. The next thing we need to add is a hook to listen for when the user clicks it.

// Implement loadView to create a view hierarchy programmatically,
// without using a nib.
- (void)loadView {
       
  //allocate the view
  self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
       
  //set the view’s background color
  self.view.backgroundColor = [UIColor whiteColor];
 
  //create the button
  UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
       
  //set the position of the button
  button.frame = CGRectMake(100, 170, 100, 30);
 
  //set the button’s title
  [button setTitle:@“Click Me!” forState:UIControlStateNormal];
 
  //listen for clicks
  [button addTarget:self action:@selector(buttonPressed)
   forControlEvents:UIControlEventTouchUpInside];
 
  //add the button to the view
  [self.view addSubview:button];
}

-(void)buttonPressed {
  NSLog(@“Button Pressed!”);
}

We use the addTarget function to hook into the UIControlEventTouchUpInside event. Basically, what we’re saying here is to send that event to self and call the function buttonPressed. The @selector directive returns what’s essentially a function pointer in the Objective C world. Lastly, if we’re telling the button to send a message to a class, we had better implement a method for that message, so all we do is simply log a message to the console. You can view the console by selecting Run / Console on XCode’s menu.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>