這個問題似乎已經回答了很多,但沒有一個發佈的解決方案適用於我。所以我想我會給這個鏡頭。我有一個布爾值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)。有誰知道爲什麼他們是空的?
正確初始化按鈕....並請檢查您的condtions ... – IronManGill
不要在把最後一行的initWithFrame方法一兩件事。 NSLog(@「%@%@%@」,oneButton,xButton,x2Button)。我懷疑參考。所以請檢查並告訴我們你得到了什麼。 – CRDave
@CRDave我照你的要求做了,得到了(null)(null)(null)。我究竟做錯了什麼? – josealvarado111