UIButton을 사용하여 속성을 수정하고 UIButton을 반환하는 메서드가 있습니다. 그러나 초기화되지 않는 것 같습니다. 나는 여기서 메모리 관리에 문제가 있다고 확신하지만 그것을 고치는 방법을 정확히 알지 못한다. 런타임 오류가 발생하지 않습니다. 다음과 같이메서드에서 사용자 지정 UIButton을 반환 하시겠습니까?
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을 서브 클래스 화하는 것일 수도 있지만 이미이 경로를 따라 갔기 때문에이 문제를 해결하려고했습니다.
+1. 팩토리 메소드는 올바르게 구성된 버튼을 얻는 유일한 방법입니다. init 메소드를 사용하는 것은 언제든지 변경 될 수있는 현재 구현에 의존합니다. 버튼 유형을 설정할 방법이 없습니다 (예 : 중요한 속성). 이것이 서브 클래 싱이 적절하지 않은 이유입니다. – Eiko
아, 리팩토링에서 나는 비난 된 프레임을 이동하는 것을 잊었습니다. 네. 그랬어. @Martin Ullrich에 대한 제 응답에서 저는 buttonWithType을 실제로 사용하고 있습니다 : init. –