这个问题似乎已经得到了很多回答,但没有一个发布的解决方案对我有用。所以我想我会试一试。我有一个布尔 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,因此我不断收到“是真”和“是假”的交替序列,但每个按钮的标题永远不会更新。
附加信息:
按钮初始化如下
头文件
@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)。有谁知道为什么他们是空的?