Bug when trying to create view with 2 UIButtons
I'm attempting to create a view with two UIButtons. The code compiles without any error but the buttons don't have any labels and they can't be clicked.
-(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:butto开发者_运维问答n];
[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;
}
Don't release those buttons. They are created as autoreleased objects. As described in documentation:
You only release or autorelease objects you own. You take ownership of an object if you create it using a method whose name begins with “alloc”, “new”, “copy”, or “mutableCopy”
None of these words appear in buttonWithType:, so you don't take responsibility and you are safe to assume it's autoreleased.
As mentioned earlier, the buttons cant be named when it is withType. The code to make the words appear in the uibutton is:
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];
精彩评论