简体   繁体   English

如何使用 sender.titleForState(.Normal) 获取 Swift 3.0、ios 中按钮的当前标题!

[英]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 中获取buttontitle ,如下所示。

@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.

相关问题 Swift 3.0如何使用TableView单元格更改按钮标题 - Swift 3.0 How to Change a Button Title Using TableView Cells Swift IOS:获取当前正在播放的歌曲的标题 - Swift IOS : Get the title of current song playing 如何使用Swift 3.0使用Swift 3.0更改iOS应用程序中按钮的高度和宽度 - How to change the height and width of a button in iOS app using Swift 4.0 which is developed using Swift 3.0 如何仅使用iOS 10在iOS Swift 3.0中捕获崩溃 - How to capture crashes in iOS Swift 3.0 using only iOS 10 无法使用 sender.title(for: .selected) 从我的按钮获取标题 - can not get title from my button by using sender.title(for: .selected) Swift - 如何获取 ios 设备当前语言 - Swift - how to get ios device current language 如何获得Swift 3.0 iOS下面的图像行为 - How to get the behaviour like below image for Swift 3.0 iOS 如何在UITableView按钮中单击以获取特定的行按钮标题 - How to get particular row button title in UITableView button click in swift 如何在swift 3中获取发件人的身份 - How to get identity of sender in swift 3 当使用swift3.0的按钮动作时,如何在ios mobile中打开文档文件eg(.pdf,.doc,.docx)? - How to open the Document files e.g(.pdf,.doc,.docx) in ios mobile when a button action using swift3.0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM