Pregunta

I just got to start developing for ios 6 on xcode. 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.

I am having problems with the identifier 'statusText' which i have already declared in the .h code.

Here's my code so far, any help would be highly appreciated. thank you in advance.

my BIDViewController.h is like so

#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

#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.

¿Fue útil?

Solución

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.

You have three options:

  1. Synthesize statusText like that so you can use that ivar.
  2. instead of accessing the var directly, access the property like this self.statusText.
  3. XCode is creating it's own variable, it's probably named _statusText, access it like that to access the variable directly.

You can also use a combination of 2 and 3

Otros consejos

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];

}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top