ios - 在 iOS 上,为什么 ViewController 中的 init 不起作用?

标签 ios

在 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/

相关文章:

iphone - 无法在 UITableViewCell 中设置自定义背景颜色

ios - 如何将两个参数传给下一个ViewController?

ios - 当使用自定义 UITableViewCell 时, ImageView 框架为零

ios - UDID 和 UUID 的区别

ios - 没有占位符图像的 UITableViewCell 中的 AlamofireImage af_setImageWithURL

iOS - 应用程序委托(delegate) - 使用 CLLocationManager 调用 didBecomeActive

IOS以编程方式插入布局

ios - UIScrollView 中 UILabel 中的像素化文本,均具有透明背景

ios - 如何防止键盘覆盖 UITextView 中的文本?

ios - 为什么使用 viewWithTag 高效?