我有一个滚动视图。我在此滚动视图中添加了一个按钮,然后将其释放。
UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
saveButton.frame = CGRectMake(415.0, 473, 80, 38);
saveButton.titleLabel.font = [UIFont fontWithName:@"Heiti TC" size:24];
[saveButton setTitle:@"" forState:UIControlStateNormal];
[saveButton setContentEdgeInsets:UIEdgeInsetsMake(2, 0, 0, 0)];
saveButton.backgroundColor = [UIColor clearColor];
[saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[saveButton setBackgroundImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
[saveButton addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
saveButton.hidden = NO;
[self.scrollview addSubview:saveButton];
[saveButton release];
当视图出现在屏幕上时应用程序崩溃,我尝试触摸屏幕的任何部分。
如果我注释掉
[saveButton release];
该应用程序完美运行。
我认为一旦将按钮添加到滚动视图中,按钮的保留计数就会增加,因此释放按钮对我来说是安全的。
这里发生了什么?将某些内容添加到滚动视图与将其添加到主视图中不同,如下所示?
[self.view addSubview:saveButton];