gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 03:50:06 26 4
gpt4 key购买 nike

这个问题似乎已经得到了很多答案,但发布的解决方案都不适合我。所以我想我应该尝试一下。我有一个 bool 值 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中的最后一行并得到(空)(空)(空)。有谁知道为什么他们是空的?

最佳答案

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

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

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

}
}

关于ios - 更改未在 Interface Builder 中创建的 UIButton 的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17163038/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com