2011-04-15 113 views
0

我试图用两个UIButton创建视图。代码编译没有任何错误,但按钮没有任何标签,并且它们不能被点击。 enter image description here尝试使用2个UIButton创建视图时出现问题

-(id)initWithTabBar { 

if ([self init]) { 
    self.title = @"Tab1"; 
    self.tabBarItem.image = [UIImage imageNamed:@"promoters.png"]; 
    self.navigationItem.title = @"Nav 1"; 
} 

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setTitle:@"Button" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(facebookAuthorize) forControlEvents:UIControlEventTouchDown]; 
[button setFrame:CGRectMake(10, 10, 100, 100)]; 

[self.view addSubview:button]; 

[button release]; 

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button2 setTitle:@"Button" forState:UIControlStateNormal]; 
[button2 addTarget:self action:@selector(facebookLogin) forControlEvents:UIControlEventTouchDown]; 
[button2 setFrame:CGRectMake(110, 10, 100, 100)]; 

[self.view addSubview:button2]; 

[button2 release]; 

return self; 
} 

回答

1

不要释放这些按钮。它们被创建为自动释放对象。正如documentation描述:

你只释放或自动释放你自己 对象。如果您使用 方法,其名称开始“黄金”, “新”,“复制”,或“mutableCopy”

的这句话没有出现在创造它,你需要的 对象所有权buttonWithType:,所以你不承担责任,你可以安全地认为它是autoreleased。

+0

哦,我明白了,so buttonWithType:是一个处理内存分配的静态类方法吗? – 2011-04-15 20:22:22

+0

我编辑了我的答案,参考文档。 – 2011-04-15 20:28:54

0

正如前面提到的那样,按钮无法在withType时命名。使这些词出现在uibutton中的代码是:

UIButton *someButton=[[[UIButton alloc]initWithFrame:CGRectMake(140, 6, 175, 36)]autorelease]; 
[someButton addTarget:self action:@selector(facebookLogin) forControlEvents:UIControlEventTouchDown]; 
[someButton setTitle:@"Button" forState:UIControlStateNormal]; 
[self.view addSubview:someButton]; 
相关问题