爵士
- (void)viewDidLoad{
for (int i = 1; i <= 4; i++)
{
playButton=[[UIButton alloc]initWithFrame:CGRectMake(curLocX,1.0,45,kScrollObjHeight)];
[playButton setBackgroundImage:[UIImage imageNamed:@"Play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playClicked) forControlEvents:UIControlEventTouchUpInside];
curLocX+=100;
}}
我已经在viewDidUnload和dealloc中发布了playButton 我创造了四个按钮,但我只发布了一次是对还是错 2.我已经发布了viewDidUnload并释放了它的对错 提前感谢您的宝贵建议 Suresh.M
答案 0 :(得分:2)
是的,如果一个对象被分配了4次,它应该以相同的计数发布。
顺便说一下,我不确定你在这里做什么,在循环结束时,playButton只会引用最后一个对象而你甚至没有将playButton作为子视图添加到任何视图中。无论如何,您可以避免使用alloc和user buttonWithType方法,此方法本身处理UIButton对象的分配和释放,因此您不必担心发布调用。
- (void)viewDidLoad{
for (int i = 1; i <= 4; i++)
{
UIButton *playButton = [UIButton buttonWithType: UIButtonTypeCustom];
playButton.frame = CGRectMake(curLocX,1.0,45,kScrollObjHeight);
[playButton setBackgroundImage:[UIImage imageNamed:@"Play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playClicked) forControlEvents:UIControlEventTouchUpInside];
curLocX+=100;
[self.view addSubview: playButton]; //you might have missed this statement
}}
答案 1 :(得分:0)
你应该从至少一个地方(viewDidUnload / dealloc)删除按钮释放,以避免为对象调用dubble release。
创建对象(在你的情况下按钮)并一次释放所有对象都没问题。
答案 2 :(得分:0)
你必须在 for 循环中释放按钮。
for (int i = 1; i <= 4; i++)
{
playButton=[[UIButton alloc]initWithFrame:CGRectMake(curLocX,1.0,45,kScrollObjHeight)];
[playButton setBackgroundImage:[UIImage imageNamed:@"Play.png"] forState:UIControlStateNormal];
[playButton addTarget:self action:@selector(playClicked) forControlEvents:UIControlEventTouchUpInside];
curLocX+=100;
[playButton release];
}
注意: 如果您分配了一个对象'n'次,则必须'n'次释放该对象。