问题
I have an array of UIButtons and want to set all their titles to a specific value at once, without looping through the array. The only solution I've found is by means of Key-Value Coding, i.e. something like this:
[self.board setValue:@"X" forKeyPath:@"titleLabel.text"];
However, the button's titleLabel property is readonly and cannot be changed. I also tried using the button's title property as the keypath, but it doesn't work either.
I've done this before by changing the "enabled" property of all the buttons at once using KVC and it worked great, but if I want to change the titles it just won't work (I'm assuming this is because of the new ControlState feature of the UIButton which allows multiple titles for its various states).
So, does anyone have a one-liner solution (with no loops) to change the title of every button from the array?
回答1:
Actually, KVC is working and is setting your text value. From the Apple Documentation: Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance of the button label So textLabel is a UILabel and the text property on UILabel is not read only. The reason it does not appear to be working is that you are only changing the text of UILabel and not the frame size of the label which has a default value of (0, 0, 0, 0). If you initialise your buttons with a default value of "(three blanks)" for instance (rather than nil) then it will work. (However, there does still seem to be an issue where iOS resets the button value to it's initial value after you click on it)
回答2:
If you are ok with subclassing, you can make your button KVC compliant for a title attribute. For your purpose, implementing -set<Key>:
method is enough, but here in Swift, you can implement both -<key>
and -set<Key>:
by defining one computed property.
class KVCButton: UIButton {
var titleForNormalState: String? {
get {
return titleForState(.Normal)
}
set {
setTitle(newValue, forState: .Normal)
}
}
}
Then just call [self.board setValue:@"X" forKeyPath:@"titleForNormalState"];
.
You can find more on ensuring KVC compliance here.
回答3:
I know this is old question, but I use a solution to set textColor for IBOutletCollection(UIButton); and It is similar to set text to button;
As John Dalton mentioned, button properties are readonly so there is no way to use KVC; But It is possible if you define a new property by category to UIButton and then add text(in my case text color) by KVC;
Add New File-> Select Objective-c File
Name: Property (it is your choice)
File Type: Category
Class: UIButton
Two file with name UIButton+Property.h/.m will be create
.h
@interface UIButton (Property)
@property (nonatomic, retain) UIColor *textColor;
- (void)setTextColor:(UIColor *)textColor;
@end
.m
#import "UIButton+Property.h"
@implementation UIButton (Property)
- (void)setTextColor:(UIColor *)textColor {
[self setTitleColor:textColor forState:UIControlStateNormal];
}
- (UIColor *)textColor {
return self.textColor;
}
@end
In class:
@property (strong, nonatomic) IBOutletCollection(UILabel) NSArray *preLoginLbls;
. . .
[_preLoginBtns setValue:TEXT_COLOR forKey:@"textColor"];
来源:https://stackoverflow.com/questions/12389410/how-do-i-change-a-uibuttons-title-using-kvc