因此,我一直试图从其他用户那里获得帮助,但我们永远无法获得任何帮助,我的语法和代码看起来很好,但无论我如何,在调用我的按钮的方法时,我都无法消除“未识别标识符”错误。我开始认为这是一个全球性的问题,而不是全球性的问题。这是我的代码和错误
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self
action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:@"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
- (void) aMethod:(id)sender
{
button[0].backgroundColor = [UIColor greenColor];
}这是我在ViewController.m文件中那个按钮的全部代码..。我有一个警告和一个错误。我的警告是
“找不到'aMethod‘的方法定义”
这个错误被标记在代码顶部的"@implementation ViewController“行下。我的错误是
未声明标识符“aMethod”的使用
这是标记在我的“-(无效) aMethod:(Id)发件人”下面
这个在我的ViewController.H文件中
- (void)aMethod;没有那个错误。我曾经尝试过在这方面寻求帮助,我不断地获得与我的编程语法相关的提示和编辑,但无论如何,我无法摆脱这些错误。还有什么可能是不对的吗?看看我剩下的节目会有帮助吗?还有一条可能有用的信息。我的整个程序是在xcode设置我的第一行之后编写的,
- (void)viewDidLoad
{
//my entire program is between these brackets.
}当我尝试这个代码"-(void) aMethod:(Id)发件人{ }“之前,”视图加载“之前,我没有得到一个错误。但是,当我把它放在"-(void) aMethod:(Id)发件人{ }“后面时,我会得到错误。当我到处乱搞找出什么问题时,我就发现了这一点。如果需要更多的信息,请告诉我。顺便说一句,我正试着用编程的方式做这件事,从来没有事先使用过storyboard...Thank!
发布于 2014-02-23 21:50:58
"method definition for 'aMethod' not found"不是指按钮,而是在头中定义了方法aMethod,但是没有在实现中实现它,因为您有-(void)aMethod:(id)sender;
编辑:
您的代码应该更像这样:
-(void)viewDidLoad {
[super viewDidLoad];
UIButton *add = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[add addTarget:self action:@selector(aMethod:)forControlEvents:UIControlEventTouchDown];
[add setTitle:@"add new" forState:UIControlStateNormal];
add.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:add];
}
- (void) aMethod:(id)sender {
button[0].backgroundColor = [UIColor greenColor];
}发布于 2014-02-23 21:48:58
在您的.h文件中更改以下内容:
- (void)aMethod;对此:
- (void)aMethod:(id)sender;视图控制器( .m文件)中的实际方法应该与此签名相匹配。
https://stackoverflow.com/questions/21975020
复制相似问题