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
- UIButton获取标题文字
- UIButton获取标题文字
- 改变UIButton上标题位置、文字颜色、背景色等
- UI for iOS(四) -- UIButton添标题文字
- UIButton标题对齐方式
- 设置UIButton标题
- uibutton 图片加文字
- UIbutton设置文字
- uibutton文字自适应。
- UIButton 文字图片排列
- UIButton文字设置问题
- uibutton透明,文字不透明
- UIbutton文字相关
- 自定义 UIButton(图片+文字)
- UIButton 文字左对齐
- UIButton 文字换行显示
- UIButton文字位置显示
- uibutton 文字闪动问题
- Innodb的内部存储结构小结
- 如何在用cmd命令安装apk文件到手机?
- Windows 10 技术预览
- Class.forName的作用以及为什么要用它
- 如何解决Visual c++不能打开文件以及工程不能添加文件的问题
- UIButton获取标题文字
- JDK Tomcat环境配置
- Socket编程
- String和StringBuffer的区别
- 编译forms程序报错,internal_impl_21.0.3.jar doesn't exist|classes.jar doesn't exist
- mysql 数据库导入frm文件
- Asp.net MVC中的ViewData与ViewBag
- c语言宏定义#define的理解与资料整理
- 臧其超(三藏)总裁王道导师简介