【发布时间】:2013-06-18 07:31:06
【问题描述】:
这个问题似乎已经得到了很多回答,但发布的解决方案都不适合我。所以我想我会试一试。我有一个布尔 posNeg ,每次它的值发生变化时,三个按钮的标题都应该改变,但它们不会。
- (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,因此我不断收到“Was True”和“Was False”的交替序列,但每个按钮的标题从未更新。
附加信息:
按钮初始化如下
头文件
@interface ...{
UIButton* oneButton;
UIButton* xButton;
UIButton* x2Button;
}
@end
实施文件
@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)。有谁知道为什么他们是空的?
【问题讨论】:
-
按钮初始化是否正确....请检查您的条件...
-
在你的 initWithFrame 方法中做一件事放在最后一行。 NSLog(@"%@ %@ %@", oneButton, xButton, x2Button)。我怀疑参考。所以检查并告诉我们你得到了什么。
-
@CRDave 我按照你的要求做了,得到 (null) (null) (null)。我做错了什么?
-
我会告诉你问题。但在那之前做一件事。删除此行 button = [UIButton buttonWithType:buttonType];并在调用 setUpButton 方法之前在 initWithFrame 中添加三行。 1. oneButton = [UIButton buttonWithType:buttonType]; 2. xButton = [UIButton buttonWithType:buttonType]; 3. x2Button = [UIButton buttonWithType:buttonType].