Dov Alperin
Dov Alperin

Reputation: 39

I get the error: instance member websitetext can not be used on type 'ViewController4

I am trying to build a iOS browser app with swift (please keep in mind I am new to swift) and in my code I get the error: instance member websitetext can not be used on type 'ViewController4' This is the only line I get an error with. None of the other forums on this error explain this to someone who is new to swift. Here is my code. Thanks!

import UIKit

class ViewController4: UIViewController, UITextFieldDelegate {

    override func viewDidLoad() {
         super.viewDidLoad()

        let requestURL = NSURL(string:website)
        let request = NSURLRequest(URL: requestURL!)
         webView.loadRequest(request)
        // Do any additional setup after loading the view.
    }

    let website = (string:websitetext.text)  //"https://google.com" line error: Instance member websitetext can not be used on type 'ViewController4

    @IBOutlet weak var webView: UIWebView!
    @IBOutlet weak var websitetext: UITextField!

    @IBAction func back(sender: AnyObject) {
        if webView.canGoBack {
            webView.goBack()
        }
    }

    @IBAction func gohome(sender: AnyObject) {
        self.performSegueWithIdentifier("webtohome", sender: self)
    }

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

Upvotes: 0

Views: 63

Answers (1)

Peter Heide
Peter Heide

Reputation: 519

You're accessing websitetext before the class is fully initialized. Do something like this, instead:

// Get rid of this declaration:
// let website = (string:websitetext.text)

override func viewDidLoad() 
{
    super.viewDidLoad()
}

override func viewWillAppear(animated: Bool) 
{ 
    if let website = websitetext.text
    {
        let requestURL = NSURL(string: website)
        let request = NSURLRequest(URL: requestURL!)
        webView.loadRequest(request)
    }
}

Upvotes: 1

Related Questions