I have a scrollview. I add a button to this scrollview and release it after.
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];
The application crashes when the view appears on screen, and I try and touch any part of the screen.
If I comment out
[saveButton release];
the application works perfectly.
I thought the retain count of the button would be incremented once I add it to the scrollview so it would be safe for me to release the button.
What’s going on here? Is adding something to a scrollview not the same as adding it to the main view like below?
[self.view addSubview:saveButton];
buttonWithType:is a convenience constructor, so it already creates an autoreleased instance, and there is no need to release the object.That means the following line of code is an error:
You should not send
release, as the instance is already autoreleased.Check the
UIButtonreference for details.