UIButton获取标题文字

来源:互联网 发布:好听的淘宝名字大全 编辑:程序博客网 时间:2024/05/23 15:06

1.问题

一个button,点击事件实现在一个搜索图标和“取消”之间的转换,那需要在响应事件中判断标题文字,所以我想获取button的标题

 代码如下:

    if ([button.titleLabel.text isEqualToString:@"取消"]) {        [button setTitle:@"" forState:UIControlStateNormal];        [button setImage:[UIImage imageNamed:@"search_normal"] forState:UIControlStateNormal];    }else {        [button setTitle:@"取消" forState:UIControlStateNormal];        [button setImage:nil forState:UIControlStateNormal];    }

问题来了,判断无效。

即我设置title为@”“时,判断

<span style="font-family: Arial, Helvetica, sans-serif;">[button.titleLabel.text isEqualToString:@"取消"]</span>

依旧成立。

2.说明

看了一眼UIButton的类,才发现事情不能想当然的认为。

因为为button赋值title实际上是将值赋给了currentTitle。

所以代码应该如是写:

    if ([button.currentTitle isEqualToString:@"取消"]) {        [button setTitle:@"" forState:UIControlStateNormal];        [button setImage:[UIImage imageNamed:@"search_normal"] forState:UIControlStateNormal];    }else {        [button setTitle:@"取消" forState:UIControlStateNormal];        [button setImage:nil forState:UIControlStateNormal];    }

3.同理

其他的赋值及获取值都可以看一下就一目了然

- (void)setTitle:(NSString *)title forState:(UIControlState)state;                     // default is nil. title is assumed to be single line- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default if nil. use opaque white- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil. use 50% black- (void)setImage:(UIImage *)image forState:(UIControlState)state;                      // default is nil. should be same size if different for different states- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state UI_APPEARANCE_SELECTOR; // default is nil- (void)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); // default is nil. title is assumed to be single line- (NSString *)titleForState:(UIControlState)state;          // these getters only take a single state value- (UIColor *)titleColorForState:(UIControlState)state;- (UIColor *)titleShadowColorForState:(UIControlState)state;- (UIImage *)imageForState:(UIControlState)state;- (UIImage *)backgroundImageForState:(UIControlState)state;- (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);// these are the values that will be used for the current state. you can also use these for overrides. a heuristic will be used to// determine what image to choose based on the explict states set. For example, the 'normal' state value will be used for all states// that don't have their own image defined.@property(nonatomic,readonly,retain) NSString *currentTitle;             // normal/highlighted/selected/disabled. can return nil@property(nonatomic,readonly,retain) UIColor  *currentTitleColor;        // normal/highlighted/selected/disabled. always returns non-nil. default is white(1,1)@property(nonatomic,readonly,retain) UIColor  *currentTitleShadowColor;  // normal/highlighted/selected/disabled. default is white(0,0.5).@property(nonatomic,readonly,retain) UIImage  *currentImage;             // normal/highlighted/selected/disabled. can return nil@property(nonatomic,readonly,retain) UIImage  *currentBackgroundImage;   // normal/highlighted/selected/disabled. can return nil@property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0);  // normal/highlighted/selected/disabled. can return nil


0 0
原创粉丝点击