comm.dan
comm.dan

Reputation: 11

How to put override func before viewDidLoad()?

Open ViewController.swift for editing, and before viewDidLoad() put the code below the question into the default code of ViewController.Swift? How do I put these 4 lines of code below using the directions above into the default code for ViewController.Swift?

override func loadView() {
    webView = WKWebView()
    webView.navigationDelegate = self
    view = webView
}

Default code ViewController.Swift:

import UIKit  

class ViewController: UIViewController {  
    override func viewDidLoad() {  
        super.viewDidLoad()  
    }

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

Upvotes: 1

Views: 966

Answers (2)

7stud
7stud

Reputation: 48649

import UIKit  

class ViewController: UIViewController { 

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

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

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

However, it matters not where the func goes in the class:

import UIKit  

class ViewController: UIViewController { 
    override func viewDidLoad() {  
        super.viewDidLoad()  
    }

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

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }
}

Upvotes: -1

Arkku
Arkku

Reputation: 42149

I'm guessing you have a storyboard in which ViewController is defined. If so, you must not override loadView():

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

(from UIViewController class reference)

Instead edit the class of the view in the storyboard and set it to WKWebView. To get the webView reference, create it as an outlet:

@IBOutlet weak var webView: WKWebView!

And link it as a referencing outlet to the view in the storyboard.

(Or you could just put the WKWebView inside the default UIView; would be simpler. You could do this either programmatically in viewDidLoad() or via the storyboard.)

Or if you don't have a storyboard, then just copypaste the code into the editor inside the class but outside the existing functions?

Upvotes: 2

Related Questions