ios - 按钮不起作用(Swift)

标签 ios swift button

我正在开发一个应用程序,请在下面找到我的代码:

import UIKit

class ViewController: UIViewController {

@IBOutlet weak var NameTextLabel: UILabel!
@IBOutlet weak var ExplainTextLabel: UILabel!
@IBOutlet weak var PriceTextLabel: UILabel!
@IBOutlet weak var FeaturesTextLabel: UILabel!

let Features = ["Features Include: Siri acts as a musicologist, Advanced searches..., Works with Apple Music, etc", " Feautres Include: Up to 18 Cores, Thunderbolt 3, 5k Display, etc",
    "Feautres Include: AR technology, I messages wont take up storage, A redesinged appstore, etc", "New Faces, Siri Face, Better Pairing Process"]

let Explain = ["Apple Smart Speaker", "Most Powerfull Mac", "The New OS", "Watch OS4"]

let Price = ["At $349", "At $4999", "Free", "Free"]

let Name =  ["Homepod", "iMac Pro", "iOS 11", "Watch OS4"]

@IBAction func MoreDidTap(sender: AnyObject) {

    let randomNumber = Int(arc4random()) % 4

    let NewName = Name [randomNumber]
    let NewExplain = Explain [randomNumber]
    let NewPrice = Price [randomNumber]
    let NewFeatures = Features [randomNumber]

    NameTextLabel.text = NewName
    ExplainTextLabel.text = NewExplain
    PriceTextLabel.text = NewPrice
    FeaturesTextLabel.text = NewFeatures
}

}

当我在模拟器中运行应用程序时,我发现单击按钮时什么都不会改变。如果有人知道请帮助我。

最佳答案

您是否将 IBAction 连接到 touchUpInside 事件的 UIButton

必须设置一个按钮才能通知事件。我们可以定义一个目标和一个选择器来获取该事件。目标代表处理事件的对象,选择器代表作用于事件的方法。

您可以通过连接 UIButton (在 Storyboard/Xib 中)和 IBAction (在类里面)或通过使用 UIButton API 以编程方式来完成此操作 - addTarget(_:action:for:)

关于ios - 按钮不起作用(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44642244/

相关文章:

ios - UINavigationBar下如何添加灰色的描述栏?

ios - 将 View 限制在状态栏底部

iphone 按钮文本颜色在单击时自行改变?

CSS 和按钮 : How to override native CSS of html buttons element?

ios - 不同 View Controller 中的相同 View ?

ios - if语句没有显示结果?

iphone - 如何在我的应用程序启动后立即自动播放音乐?

SwiftSpinner 不适用于 UIViewController。

Swift:为变量生成标识符

python - 有没有办法在不触摸 tkinter/python 的情况下按下按钮?