0

xcodeでiOS6の開発を始めたところです。しかし、初心者の開発者として、私は問題に直面しました。書籍「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

私は本を​​読みましたが、なぜこれが起こるのか理解できないようです、plsは助けます。

4

2 に答える 2

2

さて、初心者にとって、これは1行だけである必要があります

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

これで、プロパティを宣言するときに、次のように合成しない限り、そのような名前の変数は取得されません。

@implementation Class

@synthesize propertyName;

@end

そのため、statusTextは存在しません。

3つのオプションがあります。

  1. statusTextそのように合成して、そのivarを使用できるようにします。
  2. varに直接アクセスする代わりに、このようにプロパティにアクセスしますself.statusText
  3. XCodeは独自の変数を作成しています。おそらく名前が付けられ_statusTextており、そのようにアクセスして変数に直接アクセスします。

2と3を組み合わせて使用​​することもできます

于 2012-09-25T16:15:24.487 に答える
0

I am using the same book and had that problem, too. I have learned to use an underscore in the .m file

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

}
于 2013-03-01T19:31:55.813 に答える