May 25

This is my first entry in a new category called “cheat sheet.”

I’m trying to build an iPhone GUI for an application. Every time I want to do something simple – like add a button – I need to pull out my books on the subject and filter through several pages to remind myself how to do it. So I’m putting the info here for myself and anyone else who finds the process unintuitive.

Let’s assume that you are adding a button to an existing UIViewController. The first thing that you need to do is add a handler for the button press to your controller class.

Your *.h file would look something like this:

#import  
 
@interface HomeViewController : UIViewController {
 
}
 
-(IBAction) configButtonPressed:(id)sender;
 
@end

Your *.m (or *.mm) file would contain the corresponding method like this:

-(IBAction) configButtonPressed:(id)sender
{
	NSLog( @"HomeViewController:configButtonPressed:sender" );
}

 

  1. Fire up Interface Builder by double-clicking on your *.xib file.  I’m assuming you already have a View set up.
  2. I’m also assuming that  Tools / Inspector and Tools / Library show when you click on the View.
  3. Under Library / Cocoa Touch Plugin / Inputs & Values drag a Round Rect Button on to your View.
  4. Double-click on the button and fill in a label.
  5. Click elsewhere on the View to get out of label editing mode and single-click the button again.
  6. Select the second tab in the Inspector (or press Apple-Key-2) – the title of the Inspector should say Button Connections.
  7. Put your mouse over the circle to the right of event labeled Touch Up Inside.
  8. Drag the mouse over the File’s Owner icon in the *.xib window.
  9. When you release the mouse, select the method that you added to your controller class (in this example configButtonPressed).
  10. Save the *.xib file, and select Build and Go to make sure you wired the button correctly.
  11. In this example when you click the button it should log something to the Debugger Console.

Tags: , ,

Leave a Reply