简体   繁体   English

xcode上未声明的标识符

[英]undeclared identifier on xcode

I just got to start developing for ios 6 on xcode. 我刚开始在xcode上开发ios 6。 But as a novice developer, i have come into a problem. 但作为一名新手开发者,我遇到了一个问题。 Following the guide in the book 'beginning ios5 development: exploring the ios sdk' on chapter 3, the 'Button fun' example. 按照第3章“开始ios5开发:探索ios sdk”一书中的指南,“按钮乐趣”示例。

I am having problems with the identifier 'statusText' which i have already declared in the .h code. 我遇到了我已经在.h代码中声明的标识符'statusText'的问题。

Here's my code so far, any help would be highly appreciated. 到目前为止,这是我的代码,任何帮助都将受到高度赞赏。 thank you in advance. 先感谢您。

my BIDViewController.h is like so 我的BIDViewController.h是这样的

#import <UIKit/UIKit.h>

@interface BIDViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *
statusText;
- (IBAction)buttonPress:(UIButton *)sender;

@end`

and my BIDViewController.m is like so 而我的BIDViewController.m是这样的

#import "BIDViewController.h"

 @interface BIDViewController ()

 @end

 @implementation BIDViewController

 - (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
 }

 - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

 - (IBAction)buttonPress:(UIButton *)sender {
    NSString *title = [sender titleForState:<#(UIControlState)#>];
    statusText.text = [NSString stringWithFormat:@"%@ button pressed.", title];
}
@end

i have followed the book but can't seem to understand why this occurs, pls help. 我跟着这本书,但似乎无法理解为什么会发生这种情况,请帮忙。

Well, for starters, this should be only one line 那么,对于初学者来说,这应该只有一行

@property (weak, nonatomic) IBOutlet UILabel *statusText;

Now, when you declare a property, you don't get a variable named like that unless you synthesize it like this: 现在,当你声明一个属性时,你没有得到一个这样的变量,除非你像这样合成它:

@implementation Class

@synthesize propertyName;

@end

That's why statusText doesn't exist. 这就是statusText不存在的原因。

You have three options: 你有三个选择:

  1. Synthesize statusText like that so you can use that ivar. 像这样合成statusText ,这样你就可以使用那个ivar。
  2. instead of accessing the var directly, access the property like this self.statusText . 而不是直接访问var,访问像self.statusText这样的属性。
  3. XCode is creating it's own variable, it's probably named _statusText , access it like that to access the variable directly. XCode正在创建它自己的变量,它可能名为_statusText ,可以直接访问变量。

You can also use a combination of 2 and 3 您还可以使用2和3的组合

I am using the same book and had that problem, too. 我正在使用同一本书并且也遇到了这个问题。 I have learned to use an underscore in the .m file 我学会了在.m文件中使用下划线

- (IBAction)buttonPressed:(UIButton *)sender {
    NSString *title = [sender titleForState:UIControlStateNormal];
    **_statusText**.text = [NSString stringWithFormat:@"%@ button pressed.", title];

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM