Sunday, December 5, 2010

Learning XCode 1: Add a Button and 2 Simple Events

Typically you will want to add a button using the Interface Builder and then add the button’s event handlers in Xcode. Here are the steps:
  1. In Xcode double click on the NIB file that defines the view to which you wish to add a button. This will start the Interface Builder (IB).
  2. In IB, open the Library tool and drag a button to the view. Position and resize this button and give it a title.
  3. Now you need to connect the button to the View Controller class so that you can control and customize this button from Xcode. You can control-click on the button and drag the connection line to the File’s Owner in the xib view (alternatively, you can start the IB’s Inspector, select the Button Connection tab and create a Referencing Outlet – drag the connection similar to before).
  4. In Xcode you’ll need to define the button to the view controller’s interface file and then define the methods to customize the button and define its events handlers.
In this sample, I’ve added a UILabel and a UIButton class so that when you click on the button the value of the label is changed.
1@interface TestViewController : UIViewController {
2    IBOutlet UILabel *mylabel;
3    IBOutlet UIButton *mybutton;
4}
5@property (nonatomic, retain) UILabel *mylabel;
6@property (nonatomic, retain) UIButton *mybutton;
7@end
In the implementation file, you need “synthesize” the label and button objects.
1@implementation TestViewController
2 
3@synthesize mylabel;
4@synthesize mybutton;
Next I try to customize the label on the button and add an event handler.
1- (void)viewDidLoad {
2        [super viewDidLoad];
3    mylabel.text = @"www.aquacue.com";
4    [mybutton setTitle:@"Aquacue - Normal" forState: (UIControlState)UIControlStateNormal];
5    [mybutton setTitle:@"Aquacue - Highlighted" forState: (UIControlState)UIControlStateHighlighted];
6    [mybutton addTarget:self action:@selector(myButtonClick:) forControlEvents:(UIControlEvents)UIControlEventTouchDown];
7}
Now here is the definition of the handler:
1-  (void)myButtonClick:(id)sender {
2    mylabel.text = @"Clicked";
3}
Next I add another event handler so that when you click on the screen background, the value of the label is changed:
1- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
2    mylabel.text = [NSString stringWithFormat: @"Hello Aquacue %@ %d", @"AAA", event.timestamp];
3    [super touchesBegan:touches withEvent:event];
4}
You can also use the IB to define event handlers (aka Actions). The easiest way is to actually define the handler/action in Xcode using the IBAction declaration in the interface file (add the declaration just before the @end statement).
1- (IBAction)myButtonPressed:(id)sender;
Next you’ll need to define the myButtonPressed handler in the implementation file:
1- (IBAction)myButtonPressed:(id)sender {
2    mylabel.text = @"Change the label, when user clicks on the button";
3}
Note that in this sample, I’ve defined two handlers one using theUIButton addTarget method and another using the IBAction. Both of these handlers are called when the user clicks on the button. As it turns it both of these handlers get called IBAction gets called first followed by the handler that was added via addTarget.
Only the methods defined with the IBAction declaration are exposed to the IB. You can control-click on the button object in IB and you’ll see a popup menu that defines all of the events that the object is willing to handle. Using the normal IB way, you’ll need to drag an event to the File’s Owner object to expose the event handler in Xcode.

No comments:

Post a Comment