按钮不起作用(Swift)

时间:2017-06-20 01:12:05

标签: 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
}

}

当我在模拟器中运行应用程序时,我发现在点击按钮时没有任何改变。如果有人知道那么请帮助我。

2 个答案:

答案 0 :(得分:0)

您是否已将IBActionUIButton touchUpInside事件相关联?

必须为要通知的事件设置按钮。我们可以定义一个目标和一个选择器来重新获得该事件。 target表示处理事件的对象,selector表示对事件起作用的方法。

您可以通过连接UIButton(在Storyboard / Xib中)和IBAction(在课堂中)或以编程方式,使用UIButton API - addTarget(_:action:for:)

来实现此目的

答案 1 :(得分:0)

您的代码运行良好         检查这个步骤         第1步: - 检查你的故事板你的按钮发送事件(例如,触摸内容)设置与否?         步骤2: - 如果有效,甚至不更改任何东西,将打印放在MoreDidTap函数中,         步骤3: - 检查您的所有标签referance出口是否从故事板正确给出?