[英]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.