简体   繁体   中英

Setting the text of a UILabel

I'm using the new Xcode beta (which might be where the issue is coming from).

I followed an online tutorial, with making a simple counter app.

The issue I have is with the line that is followed below:

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

The whole code is as followed:

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() 
    }

The issue is:

use of unresolved identifier outputlabel

Why can't my code find outputlabel ?

It must be something like this,

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

You are actually missing declaration of your label.

OR

If you are using it via @IBOutlet, make sure you have connected outputlabel in NIB/Storyboard file.

OR

Recheck your declaration, it might be different like outputLabel and you are using it as 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)

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM