设置UILabel的文本

时间:2015-06-25 13:36:07

标签: ios swift cocoa-touch uiviewcontroller uilabel

我正在使用新的Xcode测试版(可能是问题的来源)。

我按照在线教程制作了一个简单的计数器应用程序。

我遇到的问题是下面的行:

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

2 个答案:

答案 0 :(得分:3)

必须是这样的,

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

您实际上缺少标签声明。

OR

如果您通过@IBOutlet使用它,请确保您已在NIB / Storyboard文件中连接outputlabel

OR

重新检查您的声明,它可能与outputLabel不同,您将其用作outputlabel

答案 1 :(得分:0)

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)

 }