2013-06-18 33 views
0

這個問題似乎已經回答了很多,但沒有一個發佈的解決方案適用於我。所以我想我會給這個鏡頭。我有一個布爾值posNeg,每次值改變時,三個按鈕的標題都會改變,但是它們不會。更改界面生成器中未創建的UIButton的文本

- (void) posNegButtonPressed{ 
    if (posNeg) { 
     posNeg = NO; 
     [oneButton setTitle:@"One" forState:UIControlStateNormal]; 
     [xButton setTitle:@"X" forState:UIControlStateNormal]; 
     [x2Button setTitle:@"X2" forState:UIControlStateNormal]; 
     NSLog(@"Was True"); 
    } 
    else{ 
     posNeg = YES; 
     [oneButton setTitle:@"-One" forState:UIControlStateNormal]; 
     [xButton setTitle:@"-X" forState:UIControlStateNormal]; 
     [x2Button setTitle:@"-X2" forState:UIControlStateNormal]; 
     NSLog(@"Was False"); 
    } 
} 

的的NSLog的被調用,所以我不斷收到的「是真的」的交替序列和「是假的」,但每個按鈕的標題永遠不會更新。

附加信息:

按鈕如下

頭文件

實現文件

@implementation ... 

- (id)initWithFrame:(CGRect)frame{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self setUpButton : oneButton : UIButtonTypeRoundedRect : @"One" :UIControlStateNormal : @"Helvetica-Bold" : 20 
         : width - width/4 + 2 * initialWidth : height/3 + 6 * (buttonHeight + spaceBetweenButtons) 
         : width/4 - 2 * initialWidth : buttonHeight : @selector(oneButtonPressed) : UIControlEventTouchUpInside]; 

     [self setUpButton : xButton : UIButtonTypeRoundedRect : @"X" :UIControlStateNormal : @"Helvetica-Bold" : 20 
         : width - width/4 + 2 * initialWidth : height/3 + 7 * (buttonHeight + spaceBetweenButtons) 
         : width/4 - 2 * initialWidth : buttonHeight : @selector(xButtonPressed) : UIControlEventTouchUpInside]; 

     [self setUpButton : x2Button : UIButtonTypeRoundedRect : @"X\u00B2" :UIControlStateNormal : @"Helvetica-Bold" : 20 
         : width - width/4 + 2 * initialWidth : height/3 + 8 * (buttonHeight + spaceBetweenButtons) 
         : width/4 - 2 * initialWidth : buttonHeight : @selector(x2ButtonPressed) : UIControlEventTouchUpInside]; 
} 

- (void) setUpButton: (UIButton *) button : (UIButtonType *) buttonType : (NSString *) title : (UIControlState *) controlState : (NSString *) font : (int) size : (double) xPos (double) yPos : (double) width : (double) height : (SEL) selectorMethod :(UIControlEvents *) controlEvent 
{ 
    button = [UIButton buttonWithType:buttonType]; 
    button.frame = CGRectMake(xPos, yPos, width, height); 
    [button setTitle:title controlState ]; 
    [button addTarget:self action: selectorMethod forControlEvents:controlEvent]; 
    [self addSubview:button]; 
    button.titleLabel.font = [UIFont fontWithName:font size:size]; 
} 

@end 

我想[mybutton setNeedsDisplay]但沒有工作,要麼

進行初始化

任何想法? 我的按鈕沒有正確初始化?

編輯1:根據要求添加NSLog(@"%@ %@ %@", oneButton, xButton, x2Button);initWithFrame的最後一行,並獲得(null)(null)(null)。有誰知道爲什麼他們是空的?

+0

正確初始化按鈕....並請檢查您的condtions ... – IronManGill

+0

不要在把最後一行的initWithFrame方法一兩件事。 NSLog(@「%@%@%@」,oneButton,xButton,x2Button)。我懷疑參考。所以請檢查並告訴我們你得到了什麼。 – CRDave

+0

@CRDave我照你的要求做了,得到了(null)(null)(null)。我究竟做錯了什麼? – josealvarado111

回答

0

這是由價值和指針傳遞的問題。

當調用此:

[self setUpButton : oneButton : UIButtonTypeRoundedRect : @"One" :UIControlStateNormal : @"Helvetica-Bold" : 20 
         : width - width/4 + 2 * initialWidth : height/3 + 6 * (buttonHeight + spaceBetweenButtons) 
         : width/4 - 2 * initialWidth : buttonHeight : @selector(oneButtonPressed) : UIControlEventTouchUpInside]; 

oneButton的值作爲值而不是指針(它是無是另一個問題)通過。

所以,你做什麼改變(UIButton *)按鈕不會反映到oneButton或其他兩個。

所以如果你想使用不同的方法來做到這一點比返回參考。

試試這個:

- (UIButton *) setUpButton: (UIButtonType *) buttonType : (NSString *) title : (UIControlState *) controlState : (NSString *) font : (int) size : (double) xPos : (double) yPos : (double) width : (double) height : (SEL) selectorMethod :(UIControlEvents *) controlEvent 
{ 
    UIButton * button = [UIButton buttonWithType:buttonType]; 
    button.frame = CGRectMake(xPos, yPos, width, height); 
    [button setTitle:title controlState ]; 
    [button addTarget:self action: selectorMethod forControlEvents:controlEvent]; 
    [self addSubview:button]; 
    button.titleLabel.font = [UIFont fontWithName:font size:size]; 
    return button; 
} 

,並調用它像:

oneButton = [self setUpButton : UIButtonTypeRoundedRect : @"One" :UIControlStateNormal : @"Helvetica-Bold" : 20 
         : width - width/4 + 2 * initialWidth : height/3 + 6 * (buttonHeight + spaceBetweenButtons) 
         : width/4 - 2 * initialWidth : buttonHeight : @selector(oneButtonPressed) : UIControlEventTouchUpInside]; 

評論我,如果你面對任何問題。

所有最優秀的....

+0

非常感謝您的幫助。您的修改解決了我的問題。我被價值和指針傳遞困惑,但它現在是有道理的。 – josealvarado111

0

如果是通過XIB創建的,那麼您需要爲每個按鈕設置IBOutlet。我認爲你犯了愚蠢的錯誤。所以請檢查您的IBOutlet是否連接。因爲在這個代碼中沒有錯誤。

1

請爲按鈕設置IBOutlet中,如果你使用的是廈門國際銀行,下面的代碼爲我工作

- (IBAction)sliderButtonAction:(UIButton *)sender { 

    [self.suggestedTableView reloadData]; 
    if ([sender.titleLabel.text isEqualToString:@"<<"]) { 
     [sender setTitle:@">>" forState:UIControlStateNormal]; 
    } 
    else { 
     [sender setTitle: @"<<" forState: UIControlStateNormal]; 

    } 
} 
相關問題