A crash that I can fix, but I don’t understand why this is happening

I have a scrollview. I add a button for this scroll 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 is displayed on the screen, and I try to touch any part of the screen.

If I comment

[saveButton release];

The application works fine.

I thought that the number of button holds would increase as soon as I added it to the scrollview so that it would be safe to release the button.

What's going on here? Is adding something to scrollview not the same as adding it to the main view, as shown below?

[self.view addSubview:saveButton];
+3
source share
5 answers

buttonWithType: is a convenience constructor, so it already creates an instance with auto-implementation, and there is no need to free an object.

, :

[saveButton release];

release, .

UIButton .

+10
UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];

, , . , , , , .

+3

alloc/init/new, . - UIButton *savebutton = [[UIButton alloc]init];, release, : [saveButton release];

+3

,

[UIButton buttonWithType:UIButtonTypeCustom]

, ( ). , ( ). , , , .

- ( ).

iOS .

+1

. .

UIButton * saveButton = [UIButton buttonWithType:UIButtonTypeCustom];

, . .

0
source

All Articles