Hi all,
I'm brand new to xcode and need a little help with something very basic.
I have a xib file with two buttons and a label. The label is connected to an IBOutlet and both buttons to the same IBAction. The IBAction simply changes the label to "Test". So whichever button I press, the label changes to "Test". So far so good, this works fine and the code is as follows:
ButtonsAppDelegate.h
Code:
#import <UIKit/UIKit.h>
@interface ButtonsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UILabel *label1;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
- (IBAction)buttonPress;
@end
ButtonsAppDelegate.m
Code:
#import "ButtonsAppDelegate.h"
@implementation ButtonsAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (IBAction)buttonPress;
{
label1.text = @"Test";
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
Next I want to work out which button triggered the IBAction, so I can change the label text to something different depending on which button is pressed (I'll probably just change the label text to the specific button's title).
My understanding is that the first thing I have to do is to get the "sender" of the IBAction. However, adding this code causes the app to no longer work. Clicking on a button seems to terminate the app with an uncaught exception.
The code is as follows. I appreciate that I'm not actually trying to use the sender to do what I described above - that was going to be the next step. For now I just want to understand why adding the small amount of code that I have has broken the method.
ButtonsAppDelegate.h
Code:
#import <UIKit/UIKit.h>
@interface ButtonsAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
IBOutlet UILabel *label1;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
- (IBAction)buttonPress: (id)sender;
@end
ButtonsAppDelegate.m
Code:
#import "ButtonsAppDelegate.h"
@implementation ButtonsAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Override point for customization after application launch
[window makeKeyAndVisible];
}
- (IBAction)buttonPress: (id)sender{
label1.text = @"Test";
}
- (void)dealloc {
[window release];
[super dealloc];
}
@end
Any help would be much appreciated.
Many thanks,
Gary