2017-06-20 93 views
0

我工作的一個應用程序,並請找我下面的代碼:按鈕無效(SWIFT)

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 
} 

}

當我運行在模擬器的應用和我發現,雖然點擊按鈕然後沒有什麼會改變。如果有人知道,那麼請幫助我。

+0

你有沒有在'MoreDidTap'中的代碼中放置一個斷點?它會被叫嗎? –

+0

標籤和按鈕事件綁定是否正確或不檢查一次? –

回答

0

您是否已將IBAction連接到UIButton用於touchUpInside事件?

必須爲要通知的事件設置按鈕。我們可以定義一個目標和一個選擇器來恢復該事件。目標表示處理事件的對象,選擇器表示作用於事件的方法。

您可以通過連接UIButton(在情節提要/ XIB)和IBAction(類),或者通過編程方式,做到這一點使用一個UIButton API - addTarget(_:action:for:)

0

你的代碼工作良好 檢查這個步驟 步驟1: - 檢查你的故事板你的按鈕發送事件(例如Touchupinside)設置或不? 第二步: - 將打印放在MoreDidTap函數中,如果它的工作甚至沒有改變任何東西, 第三步: - 檢查你的所有標籤參考Outlet是否從storyboard中正確給出?