[英]How to get the current Title of a button in Swift 3.0 , ios using sender.titleForState(.Normal)!
I tried to get the title
of a button
in swift like below.我试图在 swift 中获取
button
的title
,如下所示。
@IBAction func buttonAction(_ sender: Any) {
let buttonTitle = sender.titleForState(.Normal)!
}
but it didn't work,even it doesn't give any hint
when we press .
但它没有用,即使我们按下时它也没有给出任何
hint
.
after the sender.在发件人之后。
so what is the correct way of doing this in swift 3.0
那么在 swift 3.0 中执行此操作的正确方法是什么
Or else if we create an IBOutlet
and then we use its currentTitle
, it works fine like below.或者,如果我们创建一个
IBOutlet
然后使用它的currentTitle
,它会像下面这样正常工作。 Why we cannot get it with sender.
为什么我们无法通过
sender.
for above以上
@IBOutlet var thebutton: UIButton!
@IBAction func buttonAction(_ sender: Any) {
let buttonTitle = thebutton.currentTitle!
print(buttonTitle)
}
Because parameter sender is in type Any
instead of UIButton
.因为参数发送者的类型是
Any
而不是UIButton
。 Change the method signature to:将方法签名更改为:
@IBAction func buttonAction(_ sender: UIButton) {
if let buttonTitle = sender.title(for: .normal) {
print(buttonTitle)
}
}
and you should be good to go.你应该很高兴去。
To get the title of the button regardless of its current state in swift 3.0 try using this:要在 swift 3.0 中获取按钮的标题,而不管其当前状态如何,请尝试使用以下命令:
@IBAction func buttonPressed(_ sender:UIButton){
let buttonTitle = sender.titleLabel?.text
print("\(String(describing: buttonTitle)")
}
This will return the title for the state, based on the state that the button is current in.这将根据按钮当前所处的状态返回状态的标题。
Smartest version using Swift 5.0:使用 Swift 5.0 的最智能版本:
@IBAction func buttonAction(_ sender: UIButton) {
print(sender.currentTitle)
}
In order to resolve the warning, following code may be used.为了解决警告,可以使用以下代码。
@IBAction func buttonAction(_ sender: UIButton) {
let buttonTitle = sender.title(for: .normal) ?? String()
print(buttonTitle)
}
Since Swift 3.0, it changed from从 Swift 3.0 开始,它从
sender.titleForState(.Normal)! sender.titleForState(.Normal)! >> sender.title(for : .normal)!
>> sender.title(for : .normal)!
Change Any to AnyObject .将Any更改为AnyObject 。
@IBAction func buttonPressed(_ sender: AnyObject) {
let title = sender.title(for: .normal)!
print("\(title) button pressed")
}
@IBAction func buttonPressed(_ sender: AnyObject) {
let title = sender.title(for: .normal)
print("\(title) button pressed")
}
So this is for swift 4.0, not the last one.所以这是针对 swift 4.0 的,而不是最后一个。
If your button has a regular title you can use:如果您的按钮具有常规标题,您可以使用:
button.title(for: state) // probably UIControl.State.normal
If your UIButton
has an attributed title you'll need to use:如果您的
UIButton
具有属性标题,则需要使用:
self.attributedTitle(for: state)
Or you can use a more general extension:或者您可以使用更通用的扩展名:
extension UIButton {
func titleForState(_ state: UIControl.State = .normal) -> String? {
if let title = self.title(for: state) {
return title
}
if let attribTitle = self.attributedTitle(for: state) {
return attribTitle.string
}
return nil
}
}
Using Swift version 5.5.2, the best way I found out to print a label's title is:使用 Swift 5.5.2 版,我发现打印标签标题的最佳方法是:
@IBAction func keyPressed(_ sender: UIButton) {
print(sender.title(for: .normal)!)
}
I was receiving a warning while using sender.title(for: .normal)
.我在使用
sender.title(for: .normal)
时收到警告。 The exclamation mark (!) solved it.感叹号(!)解决了它。
I used switch statment:我使用了 switch 语句:
var buttonConter = 1变种 buttonConter = 1
@IBAction func hardlessSelected(_ sender: UIButton) { @IBAction func hardlessSelected(_ sender: UIButton) {
switch buttonConter {
case 1:
buttonConter += 1
print("Soft")
case 2:
buttonConter += 1
print("Medium")
case 3:
buttonConter += 1
print("Hard")
default:
print("Non")
}
}
} }
The simple way just to use currentTitle:仅使用 currentTitle 的简单方法:
@IBAction func hardlessSelected(_ sender: UIButton) { @IBAction func hardlessSelected(_ sender: UIButton) {
print(sender.currentTitle)
}
@IBAction func btnNextTapped(_ sender: Any) {
let title = (sender as AnyObject).title(for: .normal)!
}
Swift4.0斯威夫特4.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.