我有一个方法,它接受一个UIButton,修改它的属性并返回一个UIButton。然而,它似乎从来没有被初始化过。我确信我做了一些错误的内存管理在这里,但不知道如何解决它。没有运行时错误。
它叫like so…
newGameBtn = [self customButtonFromButton:newGameBtn
withText:[NSString stringWithFormat:@"NEW GAME"]
withFontSize:22
withBorderColor:[UIColor orangeColor]
isSilent:YES];
[dashboardContainer addSubview:newGameBtn];
方法定义如下…
- (UIButton*) customButtonFromButton:(UIButton*)button withText:(NSString*)text withFontSize:(int)fontSize withBorderColor:(UIColor*)borderColor isSilent:(BOOL)isSilent {
button = [[[UIButton alloc] init] autorelease];
// Set properties from parameters
// Other conditional custom stuff here
return button;
}
注:newGameBtn
类型为UIButton*,初始化为:newGameBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
另一个选项可能是子类UIButton,但我想我会试着修复这个,因为我已经走了这条路。
在创建按钮时应该使用+[UIButton buttonWithType:]
来获得正确初始化的按钮
大多数类不能被默认的-[NSObject init]
方法正确初始化。因此,请查看类引用或超类引用,以获得可用的初始化方法。
在这种情况下,你还应该设置一个frame
。
你没有用你的方法修改这个按钮,你用alloc-init创建了一个全新的按钮!
如果你想改变第一个参数中的按钮,只需删除方法的第一行