簡體   English   中英

設置UILabel的文本

[英]Setting the text of a UILabel

我正在使用新的Xcode beta(可能是問題所在)。

我遵循了一個在線教程,並制作了一個簡單的計數器應用程序。

我遇到的問題是下面的代碼行:

outputlabel.text = "The button has been clicked \ (currentcount) number of times"

整個代碼如下:

class ViewController: UIViewController {
     @IBOutlet weak var outputLabel: UILabel!
     var currentcount = 0

     override func viewDidLoad() {
         super.viewDidLoad()
         // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

     @IBAction func addOneButton(sender: UIButton) {

        currentcount = currentcount + 1
        outputlabel.text  = "The button has been clicked \(currentcount) number of times"
        outputLabel.textColor = UIColor.redColor() 
    }

問題是:

使用未解決的標識符outputlabel

為什么我的代碼找不到outputlabel

一定是這樣的

let outputlabel = UILabel(frame: CGRectMake(0, 0, 100, 30))
outputlabel.text = "The button has been clicked \(currentcount) number of times"

您實際上缺少標簽的聲明。

要么

如果通過@IBOutlet使用它,請確保已在NIB / Storyboard文件中連接了outputlabel

要么

重新檢查你的宣言,它可能會像不同outputLabel和你正在使用它作為outputlabel

Swift 2.0:

 override func viewDidLoad() {
  super.viewDidLoad()

  let labelName = UILabel(frame: CGRectMake(0, 0, 300, 200))
  labelName.text = "Sample Label"

  // Enum type, two variations:
  labelName.textAlignment = NSTextAlignment.Right
  labelName.textAlignment = .Right

  labelName.textColor = UIColor.redColor()
  labelName.shadowColor = UIColor.blackColor()
  labelName.font = UIFont(name: "HelveticaNeue", size: CGFloat(22))
  self.view.addSubview(labelName)

 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM