Chrisman
Chrisman

Reputation: 3

Cannot get .text Property of UITextField in Swift -- Error: "Instance member 'textFieldName' cannot be used on type 'ViewController'

All I'm trying to do is get the text input from a UITextField object on my main.storyboard. Both @IBOutlet objects show that they are linked to the correct text field objects. However, when I try to make the constant meenieOneText take on the value of the .text property of the meenieOne UITextField, I get the error message "Instance member 'meenieOne' cannot be used on type 'ViewController'.

I'm really new at this so I could be doing something simple completely wrong, but I'm at my wit's end with this because it looks right to me.

Any help is much appreciated.

Thanks in advance!

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var meenieOne: UITextField!
    @IBOutlet weak var meenieTwo: UITextField!

    let meenieOneText: String = meenieOne.text

Upvotes: 0

Views: 967

Answers (2)

tedcurrent
tedcurrent

Reputation: 431

Try this instead:

var meenieOneText: String = ""

override func viewDidLoad() {
  super.viewDidLoad()
  meenieOneText = meenieOne.text!
}

You have a problem, because in iOS the Storyboard elements, while accessible, may not have been initialised (eg. the user cannot see them) as you try to access their properties. The easiest way to ensure the elements are initialised, you would access them after viewDidLoad(). There are also other phases such as 'viewDidDisappear' and so forth. See Apple documentation on ViewControllers here: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/

Upvotes: 1

Keval Vadoliya
Keval Vadoliya

Reputation: 1053

You can not use properties of UITextField directly in class. Write same code in any function. Its work properly.

Upvotes: 0

Related Questions