更改未在Interface Builder中创建的UIButton的文本

时间:2013-06-18 07:31:07

标签: ios uibutton title

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

3 个答案:

答案 0 :(得分:1)

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

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

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

    }
}

答案 1 :(得分:0)

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

答案 2 :(得分: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 *)按钮所做的任何更改都不会反映到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];

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

一切顺利......