[signInButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
是實際的代碼,不應該在您的界面(.h文件)中進行。該界面用於原型設計和定義當地人和道具。我猜你正在做這個編程,如果你是,你不需要IBOutlet和IBAction。對於初學者來說,它可能更好地做到這一點在Interface Builder ..
你的接口(.h文件中)應該是這樣的:
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
UIButton *_signInButton;
}
@property(nonatomic,retain) UIButton * signInButton;
-(IBAction)buttonClicked :(id)sender;
@end
你的實現(.m文件)應該是這樣的:
#import "MyViewController.h"
@implementation MyViewController
@synthesize signInButton=_signInButton;
- (void)viewDidLoad {
[super viewDidLoad];
self.signInButton = [[UIButton alloc] initWithFrame:CGRectMake(X_POS, Y_POS, 30, 30)];
[self.signInButton addTarget:self action:@selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
[self.signInButton setTitle:@"PRESS ME" forState:UIControlStateNormal];
[self.view addSubview:self.signInButton];
}
-(IBAction)buttonClicked :(id)sender
{
NSLog(@"CLICKED!");
//THE BUTTON WAS CLICKED, DO STUFF
}
- (void)dealloc
{
[_signInButton release];_signInButton=nil;
}
@end
給點時間閱讀目標c的基本知識將會對你非常有益。 – Ishu 2010-12-06 12:27:21