在开发中,我们常常需要动态地改变按钮标签文字,使用 setTitle() 函数就可以了。有时我们需要在几个标题间切换,比如下面样例所示,按钮点击后按钮文字会在“播放”“暂停”间轮流切换。
2,使用titleForState()获取
要实现这个功能,首先要获取按钮当前的标题文字,有如下两种方式:
1,使用currentTitle获取
1
2
3
4
5
|
if
myBtn.currentTitle ==
"播放"
{
myBtn.setTitle(
"暂停"
, forState: .
Normal
)
}
else
{
myBtn.setTitle(
"播放"
, forState: .
Normal
)
}
|
2,使用titleForState()获取
1
2
3
4
5
|
if
myBtn.titleForState(.
Normal
) ==
"播放"
{
myBtn.setTitle(
"暂停"
, forState: .
Normal
)
}
else
{
myBtn.setTitle(
"播放"
, forState: .
Normal
)
}
|