2016-04-14 55 views
0

我想用swift構建一個iOS瀏覽器應用程序(請記住我是新來的swift),並在我的代碼中出現錯誤:實例成員websitetext不能用於'ViewController4'類型這是我遇到錯誤的唯一行。這個錯誤的其他論壇都沒有解釋這是一個新的swift的人。這是我的代碼。謝謝!我得到的錯誤:實例成員websitetext不能用於類型'ViewController4

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. 
    } 
} 
+0

那條線應該有一個'NSURL'嗎? (另外,該線應該在某個功能內嗎?) –

回答

1

您正在訪問websitetext,然後才能完全初始化類。做這樣的事情,而不是:

// 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) 
    } 
} 
相關問題