我在斯坦福大學ios7的演示應用程序的工作,現在的問題是:目標C下劃線變量和自變量不同
#import "YSHViewController.h"
@interface YSHViewController()
@property (weak, nonatomic) IBOutlet UILabel *flipsLable;
@property (nonatomic) int flipCount;
@end
@implementation YSHViewController
- (void)setFlipCount:(int)flipCount
{
_flipCount = flipCount;
_flipsLable.text =[NSString stringWithFormat:@"Flips: %d",_flipCount]; // not working
NSLog(@"%@", _flipsLable.text);
NSLog(@"%@", self.flipsLable.text);
self.flipsLable.text = [NSString stringWithFormat:@"Flips: %d",_flipCount];
}
- (IBAction)touchCardButton:(UIButton *)sender {
//if (sender.currentTitle.length != 0) {
if ([sender.currentTitle length]) {
//UIImage *cardImage = [UIImage imageNamed:@"cardback"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
} else {
//UIImage *cardImage = [UIImage imageNamed:@"cardfront"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:@"A♣︎" forState:UIControlStateNormal];
}
_flipCount++;
NSLog(@"%i",_flipCount); //
// self.flipCount++;
}
的flipLable數量不會增加,但沒有錯app.But我更改代碼與自我,它的工作原理:
#import "YSHViewController.h"
@interface YSHViewController()
@property (weak, nonatomic) IBOutlet UILabel *flipsLable;
@property (nonatomic) int flipCount;
@end
@implementation YSHViewController
- (void)setFlipCount:(int)flipCount
{
// _flipCount = flipCount;
// _flipsLable.text =[NSString stringWithFormat:@"Flips: %d",_flipCount]; // not working
// NSLog(@"%@", _flipsLable.text);
// NSLog(@"%@", self.flipsLable.text);
_flipCount = flipCount;
self.flipsLable.text = [NSString stringWithFormat:@"Flips: %d",self.flipCount];
}
- (IBAction)touchCardButton:(UIButton *)sender {
//if (sender.currentTitle.length != 0) {
if ([sender.currentTitle length]) {
//UIImage *cardImage = [UIImage imageNamed:@"cardback"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardback"]
forState:UIControlStateNormal];
[sender setTitle:@"" forState:UIControlStateNormal];
} else {
//UIImage *cardImage = [UIImage imageNamed:@"cardfront"];
[sender setBackgroundImage:[UIImage imageNamed:@"cardfront"]
forState:UIControlStateNormal];
[sender setTitle:@"A♣︎" forState:UIControlStateNormal];
}
// _flipCount++;
// NSLog(@"%i",_flipCount); //
self.flipCount++;
}
那麼,什麼是兩個變量的不同來設定fliCount的數量。
其平等,但我建議你使用self.property – ErasmoOliveira