简体   繁体   English

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

[英]Change the text of a UIButton not created in Interface Builder

This question seems to have been answered a lot but none of the posted solutions work for me. 这个问题似乎已经得到了很多回答,但是没有一个发布的解决方案对我有用。 So I thought I would give this a shot. 所以我想我会试一试。 I have a boolean posNeg and every time its value changes, the title of three buttons should change but they don't. 我有一个布尔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");
    }
}

The NSLog's are being called, so I keep getting an alternating sequence of "Was True" and "Was False" but the title of each of the buttons is never updated. 正在调用NSLog,因此我不断得到“ Was True”和“ Was False”的交替序列,但每个按钮的标题从未更新。

Additional Info: 附加信息:

Buttons were initialized as follows 按钮初始化如下

Header File 头文件

@interface ...{ 
    UIButton* oneButton;
    UIButton* xButton;
    UIButton* x2Button; 
} 
@end

Implementation File 实施文件

@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

I tried [mybutton setNeedsDisplay] but that didn't work either 我尝试了[mybutton setNeedsDisplay]但是那也不起作用

Any ideas? 有任何想法吗? Are my buttons not initialized properly? 我的按钮没有正确初始化吗?

Edit 1: Added NSLog(@"%@ %@ %@", oneButton, xButton, x2Button); 编辑1:添加了NSLog(@"%@ %@ %@", oneButton, xButton, x2Button); to the last line in initWithFrame as requested and got (null) (null) (null). 返回到initWithFrame的最后一行,并得到(null)(null)(null)。 Does anyone know why this they're null? 有谁知道为什么他们为空?

Please set IBOutlet for button if you are using XIB,below code is working for me 如果您使用的是XIB,请为按钮设置IBOutlet,以下代码对我有用

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

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

    }
}

If it is create by XIB then you need to set IBOutlet for each button. 如果它是由XIB创建的,则需要为每个按钮设置IBOutlet。 And i think you have done silly mistake. 而且我认为您犯了愚蠢的错误。 So please check your IBOutlet is connected or not. 因此,请检查您的IBOutlet是否已连接。 Because in this code there is no mistake. 因为在此代码中没有错误。

This is problem of pass by value and pointer. 这是通过值和指针传递的问题。

When you call this: 当您致电:

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

The value of oneButton is passed as value not as pointer (Which is Nil is other problem). oneButton的值作为值而不是指针传递(另一个是Nil)。

So what ever change you do with (UIButton *) button will not reflect to oneButton or other two. 因此,使用(UIButton *)按钮所做的任何更改都不会反映到oneButton或其他两个按钮。

So If you want to use different method to do this than return reference. 因此,如果您要使用其他方法而不是返回引用来执行此操作。

Try this: 尝试这个:

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

And call It Like: 并称其为:

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

Comment me if you face any problem. 如果您遇到任何问题,请评论我。

All the best.... 祝一切顺利....

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 显示在Interface Builder中的代码中创建的UIButton - Show UIButton Created in Code in Interface Builder 通过代码设置在Interface Builder中创建的UIButton的图像 - Set Image of UIButton Created in Interface Builder, by code 使用Interface Builder创建了UIButton。 现在我想使用约束来更改该按钮的位置和大小 - Created the UIButton using Interface Builder. Now i want to change the position and size of that button using Constraints 如何在Interface Builder中更改UIButton的背景图像并保持其形状? - How to change background image of the UIButton in Interface Builder and preserve its shape? 使用Interface Builder在屏幕外创建UIButton - Create UIButton offscreen with Interface Builder UITextView内的Interface Builder UIButton - Interface Builder UIButton inside of UITextView Interface Builder中对UIButton的振动效应 - Vibrancy Effect on UIButton in Interface Builder 当点击以编程方式创建的UIButton时,是否可以将其推送到在界面生成器中可编辑的控制器? - When tapping a UIButton that was created programmatically is it possible to push to a controller that is editable in interface builder? 如何以编程方式创建的uibutton以编程方式显示弹出窗口(不使用接口构建器) - How to display a popover programmatically from a uibutton that is also created programmatically (Not using interface builder) 如何在Interface Builder中使用自定义UIButton? - How to use a custom UIButton with Interface Builder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM