【问题标题】:Change the text of a UIButton not created in Interface Builder更改未在 Interface Builder 中创建的 UIButton 的文本
【发布时间】: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].

标签: ios uibutton title


【解决方案1】:

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

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

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

    }
}

【讨论】:

    【解决方案2】:

    这是传值和指针的问题。

    当你调用它时:

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

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

    万事如意....

    【讨论】:

    • 非常感谢您的帮助。您的修改解决了我的问题。我对按值和指针传递感到困惑,但现在有意义了。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2010-12-07
      • 1970-01-01
      • 2011-11-21
      • 2011-06-21
      • 2015-07-01
      • 2012-08-15
      • 1970-01-01
      • 2010-12-06
      • 1970-01-01
      相关资源
      最近更新 更多