0

这个问题似乎已经得到了很多回答,但没有一个发布的解决方案对我有用。所以我想我会试一试。我有一个布尔 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)。有谁知道为什么他们是空的?

4

3 回答 3

1

如果您使用的是 XIB,请为按钮设置 IBOutlet,下面的代码对我有用

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

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

    }
}
于 2013-06-18T07:50:54.820 回答
0

如果它是由 XIB 创建的,那么您需要为每个按钮设置 IBOutlet。我认为你犯了愚蠢的错误。所以请检查您的 IBOutlet 是否已连接。因为在这段代码中没有错误。

于 2013-06-18T07:47:06.040 回答
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 的值作为值而不是指针传递(这是 Nil 是另一个问题)。

因此,您对 (UIButton *) 按钮所做的任何更改都不会反映到一个按钮或其他两个按钮。

因此,如果您想使用与返回参考不同的方法来执行此操作。

试试这个:

- (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];

如果您遇到任何问题,请评论我。

祝一切顺利....

于 2013-06-18T10:41:48.230 回答