簡體   English   中英

返回方法中的自定義UIButton?

[英]Return customized UIButton in method?

我有一個采用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,但我認為我已經嘗試過此方法,因此我將嘗試解決此問題。

創建按鈕時,應使用+[UIButton buttonWithType:]以獲取正確初始化的按鈕。

大多數類未通過默認的-[NSObject init]方法正確初始化。 因此,請查看類引用或超類引用,以獲取可用的初始化方法。

在這種情況下,您還應該設置一個frame

您無需使用方法修改此按鈕,而是使用alloc-init創建一個全新的按鈕!

如果要更改第一個參數中的按鈕,只需刪除方法的第一行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM