xcode上未声明的标识符

时间:2012-09-25 16:01:57

标签: iphone xcode ios5 ios6 undeclared-identifier

我刚开始在xcode上开发ios 6。 但作为一名新手开发者,我遇到了一个问题。 按照第3章“开始ios5开发:探索ios sdk”一书中的指南,“按钮乐趣”示例。

我在.h代码中已经声明的标识符'statusText'有问题。

到目前为止,这是我的代码,任何帮助都将受到高度赞赏。提前谢谢你。

我的BIDViewController.h就像这样

#import <UIKit/UIKit.h>

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

@end`

和我的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

我跟着这本书,但似乎无法理解为什么会这样,请帮忙。

2 个答案:

答案 0 :(得分:2)

嗯,首先,这应该只有一行

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

现在,当你声明一个属性时,你不会得到一个这样的变量,除非你像这样合成它:

@implementation Class

@synthesize propertyName;

@end

这就是statusText不存在的原因。

您有三种选择:

  1. 合成statusText,这样就可以使用那个ivar。
  2. 而不是直接访问var,访问属性,如self.statusText
  3. XCode正在创建它自己的变量,它可能名为_statusText,可以直接访问变量。
  4. 您还可以使用2和3的组合

答案 1 :(得分:0)

我正在使用同一本书并且也遇到了这个问题。我学会了在.m文件中使用下划线

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

}