ios - 在 Action 中设置按钮文本

标签 ios swift

我有一个按钮,我想在其中以编程方式设置文本。

至于现在我有以下几点:

@IBAction func saveButtonClicked(_ sender: AnyObject) {
    sender.setTitle("Action", for: UIControlState.normal)
    closeKeyboard()
}

问题是,这是一个 Action socket ,所以当我按下按钮时,文本只会附加到按钮上。

我试图将 setTitle 放在 viewdidload 函数中,但它无法编译。 我如何使文本不变?我必须制作多个导出,还是可以通过其他方式完成?

最佳答案

首先创建一个名为 button_1 的 Outlet。

@IBOutlet var button_1: UIButton!

在viewDidLoad中添加这个

button_1.setTitle("Button text", forState: .Normal)

要在单击按钮时触发操作,请使用:

 @IBAction func ButtonClicked(sender: UIButton){
        button_1.setTitle("You clicked me", forState: .Normal)
    }

关于ios - 在 Action 中设置按钮文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39932602/

相关文章:

ios - 如何在 Swift 中覆盖核心数据属性

ios - 如何在不安装cocoapods的情况下运行从github下载的xcode项目?

ios - 替代 iOS 9 的 UIAlertView?

swift - 如何在Realm数据库中保存多级关系

ios - 图像不显示在 Firebase 的表格单元格中

ios - 自定义 UITableViewCell 某些 IBOutlet 元素为零

ios - 滑动 Uiimageview pffile 数组

ios - Roboto 字体在 iOS 13 中的 iPhone XR 上无法使用

ios - 使用 Codable 协议(protocol)按照从服务器接收到的相同顺序解码 json

ios - 我如何向使用点击手势识别器的图像添加事件指示器