在 ViewController 的界面中,我有
@property int count;
在实现中,我有
@synthesize count;
-(id) init {
self = [super init];
if (self) {
self.count = 100;
}
return self;
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"%i", self.count++);
}
但由于某种原因,第一次
self.count
打印出来了,是0而不是100?
最佳答案
将在您的 UIViewController 上调用各种 -init 方法之一,具体取决于它是来自 .xib、 Storyboard 还是在代码中的其他位置手动分配。
放置这种初始化的更好的地方是-viewDidLoad,像这样
- (void)viewDidLoad {
[super viewDidLoad];
self.count = 100;
}
关于ios - 在 iOS 上,为什么 ViewController 中的 init 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256055/