Jul 16, 2008

(IPhone) How to catch a button's click event

Here is an example:

//ViewBasedAppDelegate.h
#import

@class ViewBasedViewController;

@interface ViewBasedAppDelegate : NSObject {
UIWindow *window;

}

@property (nonatomic, retain) UIWindow *window;
-(void) catchButton;

@end

//ViewBasedAppDelegate.m
#import "ViewBasedAppDelegate.h"

@implementation ViewBasedAppDelegate
@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(10.0, 90.0, 160.0, 40.0);
[button setTitle:@"Button" forState:UIControlStateNormal];
[button addTarget:(id)self action:@selector(catchButton)
forControlEvents:UIControlEventTouchUpInside];
[window addSubview:button];
[window makeKeyAndVisible];

}

- (void)catchButton{
NSLog(@"button pressed");

}

- (void)dealloc {
[window release];
[super dealloc];
}

@end

1 comment:

呂毅樂lvyile said...

Thx, I got a simple lesson