我正在尝试制作一个带有按钮、得分计数器和计时器的简单应用程序,但出现了一些错误
#import <UIKit/UIKit.h>
@interface xyzViewController : UIViewController
{
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;
NSInteger count;
NSInteger seconds;
NSTimer *timer;
}
- (IBAction)buttonPressed //Expected ';' after method prototype
{
count++
scoreLabel.text = [NSString stringWithFormat:@"Score \n %i", count]
}
@end
如果我添加“;”我得到这个:
- (IBAction)buttonPressed;
{ //Expected identifier or '('
count++
scoreLabel.text = [NSString stringWithFormat:@"Score \n %i", count]
}
@end
我必须做什么?
你混淆了interface
and implementation
。界面包含
(全局可见)实例变量、属性和方法声明, IE。
原型:
@interface xyzViewController : UIViewController
{
IBOutlet UILabel *scoreLabel;
IBOutlet UILabel *timerLabel;
NSInteger count;
NSInteger seconds;
NSTimer *timer;
}
- (IBAction)buttonPressed;
@end
该方法本身进入实现:
@implementation xyzViewController
- (IBAction)buttonPressed
{
count++;
scoreLabel.text = [NSString stringWithFormat:@"Score \n %i", count];
}
@end
Remarks:
- 约定是类名以大写字母开头:
XyzViewController
.
-
Create 特性对于商店(如果您还没有):
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
编译器合成实例变量_scoreLabel
自动,因此您不需要在界面中使用它。然后通过以下方式访问该属性
self.scoreLabel.text = ....;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)