UIButton 标题更改为默认值

2023-07-07移动开发问题
8

本文介绍了UIButton 标题更改为默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我觉得这可能是一个愚蠢的问题......但无论如何我有这种奇怪的 UIButton 标题行为.

I feel like this is probably a stupid question... but anyway I have this kind of weird UIButton title behavior.

按钮已设置并连接到 IB 中的动作和属性(动作是 startButtonPushed,属性是 startButton).在视图控制器内部,我的操作设置如下:

The button is set up and connected to both an action and a property in IB (the action is startButtonPushed and the property is startButton). Inside the view controller I have the action set up like this:

bool buttonStateStop;

- (IBAction)startPushed:(id)sender 
{
    if (buttonStateStop) 
    {
        [appD.locationManager stopSavingLocations];
        startButton.titleLabel.text = @"Start";
        buttonStateStop = NO;
    }
    else 
    {
        [appD.locationManager startSavingLocations];
        startButton.titleLabel.text = @"Stop";
        buttonStateStop = YES;        
    }
}

最初我将 IB 中的默认标题设置为开始",但每当我按下按钮时,它会在几分之一秒内变为停止",然后返回.我花了一段时间试图弄清楚为什么标题总是回到开始".最终,我将 IB 标题更改为xxxxxx",并意识到无论如何,按钮标题更改后,IB 标题都会立即重新声明.

Originally I had the default title in IB set to "Start" but whenever I pressed the button it would change to "Stop" for a fraction of a second and then back. I spent a while trying to figure out why the title kept getting set back to "Start". Eventually I changed the IB title to "xxxxxx" and realized that no matter what, the IB title gets reasserted immediately after the title of the button changes.

所以问题是:为什么 IB 不断将按钮的标题改回默认值?我以前从未遇到过这种行为.并且(显然)我该如何解决?

So the question is: why does IB keep changing the button's title back to default? I've never come across this behavior before. And (obviously) how can I fix it?

额外信息:对按钮的唯一引用是 @property@synthesize 和上面代码中的语句.视图位于导航控制器内部.

Extra info: the only references to the button are the @property, @synthesize, and the statements in the code above. The view is inside of a navigation controller.

推荐答案

你需要使用 setTitle:forState: 方法,而不是设置 titleLabel.text 属性:

You need to use setTitle:forState: method instead of setting the titleLabel.text property:

[startButton setTitle:@"Start" forState:UIControlStateNormal];
// Normal and highlighted titles do not need to be the same
[startButton setTitle:@"Start!" forState:UIControlStateHighlighted];

现在发生的情况是,您在标签中设置了代表当前状态视图的标题,但是一旦状态从 push 变为 normal,按钮会将标签重置为新状态的标题(即您在 IB 中设置的文本).

What happens now is that you set the title in the label that represents the view of the current state, but once the state changes from pushed to normal, the button resets the label back to the title for the new state (which is the text that you set in the IB).

这篇关于UIButton 标题更改为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

硬件音量按钮更改应用程序音量
Hardware Volume buttons change in app volume(硬件音量按钮更改应用程序音量)...
2024-08-12 移动开发问题
10

Cocos2d - 如何检查不同层中对象之间的交集
Cocos2d - How to check for Intersection between objects in different layers(Cocos2d - 如何检查不同层中对象之间的交集)...
2024-08-12 移动开发问题
8

恢复游戏 cocos2d
Resume game cocos2d(恢复游戏 cocos2d)...
2024-08-12 移动开发问题
6

突出显示朗读文本(在 iPhone 的故事书类型应用程序中)
Highlight Read-Along Text (in a storybook type app for iPhone)(突出显示朗读文本(在 iPhone 的故事书类型应用程序中))...
2024-08-12 移动开发问题
9

Cocos2D + 仅禁用 Retina iPad 图形
Cocos2D + Disabling only Retina iPad Graphics(Cocos2D + 仅禁用 Retina iPad 图形)...
2024-08-12 移动开发问题
10

如何将 32 位 PNG 转换为 RGB565?
How to convert 32 bit PNG to RGB565?(如何将 32 位 PNG 转换为 RGB565?)...
2024-08-12 移动开发问题
21