error: expected specifier-qualifier-list before '[' token ....... for UIButton in XCode
I am getting the following error while building the code for creating the buttons in XCode:
error: expected specifier-qualifier-list before '[' token ....开发者_StackOverflow社区... for UIButton in XCode
Following is the code :
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
UIButton *signInButton;
[signInButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
-(IBAction)buttonClicked : (id)sender;
@end
Any suggestions to solve the error ?
Thanks in advance
You're putting implementation code into the interface declaration. That's not where it should be.
The button should be declared in the interface and then implemented in the implementation block in your .m file.
I suggest you pick up a book on iOS development, perhaps the Big Nerd Ranch guide to iPhone Development?
[signInButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
is actual code, and shouldn't go in your interface (.h file). The interface is for prototyping and defining locals and props. Im guessing your doing this programatically, if you are, you don't need the IBOutlet, and IBAction. For beginners, its probably better to do this in interface builder..
Your interface (.h file) should look like this:
#import <UIKit/UIKit.h>
@interface MyViewController : UIViewController {
UIButton *_signInButton;
}
@property(nonatomic,retain) UIButton * signInButton;
-(IBAction)buttonClicked :(id)sender;
@end
Your implementation (.m file) should look like this:
#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
Just clean and build again. It works for me
精彩评论