开发者

Return customized UIButton in method?

开发者 https://www.devze.com 2023-04-07 10:33 出处:网络
I have a method that takes a UIButton, modifies its properties and returns a UIButton. However, it doesn\'t ever seem to be initialized. I\'m sure I\'m doing something wrong with the memory management

I have a method that takes a UIButton, modifies its properties and returns a UIButton. However, it doesn't ever seem to be initialized. I'm sure I'm doing something wrong with the memory management here, but don't exactly know how to fix it. No runtime errors occur.

It is called like so...

newGameBtn = [self customButtonFromButton:newGameBtn
                                 withText:[NSString stringWithFormat:@"NEW GAME"]
                             withFontSize:22
                          withBorderColor:[UIColor orangeColor]
                                 isSilent:YES];
[dashboardContainer addSubview:newGameBtn];

The method is defined as follows...

- (UIButton*) customButtonFromButton:(UIButton*)button withText:(NSString*)text withFontSize:(int)fontSize withBorderColor:(UIColo开发者_开发百科r*)borderColor isSilent:(BOOL)isSilent {
    button = [[[UIButton alloc] init] autorelease];

    // Set properties from parameters
    // Other conditional custom stuff here

    return button;
}

NOTE: newGameBtn is of type UIButton* and is initialized with: newGameBtn = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

Another option might be to subclass UIButton, but I figured I'd try to fix this since I've already walked down this path.


You should use +[UIButton buttonWithType:] when creating buttons to get a properly initialized button.

Most classes are not properly initialized by the default -[NSObject init] method. So please look at the class reference, or superclass reference, for a usable initialization method.

In this case you should also set a frame.


You don't modify this button with your method, you're creating a completely new one with alloc-init!

If you want to change the button in your first argument just remove the first line of your method

0

精彩评论

暂无评论...
验证码 换一张
取 消