【问题标题】:undeclared identifier on xcodexcode上未声明的标识符
【发布时间】:2012-09-25 16:01:56
【问题描述】:

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

我在 .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

我已经按照这本书,但似乎无法理解为什么会发生这种情况,请帮助。

【问题讨论】:

  • 您遇到的具体问题是什么?编译器问题或运行时错误?

标签: iphone xcode ios5 ios6 undeclared-identifier


【解决方案1】:

嗯,对于初学者来说,这应该只有一行

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

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

@implementation Class

@synthesize propertyName;

@end

这就是 statusText 不存在的原因。

您有三个选择:

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

您也可以使用 2 和 3 的组合

【讨论】:

  • @emilioPalaez,你的救命稻草,非常感谢你......还有一件事,你能推荐一本好书来学习吗,那太棒了......干杯:)
  • 对不起,我不知道有什么好书。我在大学学习了基本的 C 和 Java 编程,并利用这些知识获取了一些 Apple 的文档和示例,然后开始学习。
【解决方案2】:

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多