Home>

I asked a question about how to use the Delegate method because I didn't know if my understanding was correct.
In the following code, the methodsfunc webView ~ didcommitandfunc webView ~ didfinishused when the screen starts to load are considered to be delegate methods. This delegate method is basically aclass ViewController: UIViewController, WKNavigationDelegatewhen using it, and I think it should be described as a class that inherits. From my understanding, with respect to differences with classes, you must always override the method I want to use, andwebView.navigationDelegate = selfIt must be specified whether to process in the class (in this case, since self is specified,func webViewmust be processed in ViewController) Is it correct?
If there is a mistake in understanding, I would appreciate it if you could let me know.
Thank you.

import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
  var webView = WKWebView ()
  @IBOutlet weak var indicator: UIActivityIndicatorView!
  @IBOutlet weak var toolBar: UIToolbar!

  override func viewDidLoad () {
    super.viewDidLoad ()
    // Is it an entity?

    //size
    webView.frame = CGRect (x: 0, y: toolBar.frame.size.height, width: view.frame.size.width, height: view.frame.size.height-toolBar.frame.size.height * 2)
    view.addSubview (webView)

    // what to load
    webView.navigationDelegate = self
    let request = URLRequest (url: url!)
    webView.load (request)

    // When loading starts
    func webView (_ webview: WKWebView, didCommit: WKNavigation) {
      print ("Reading start")
      indicator.startAnimating ()
    }
    // When loading is complete
    func webView (_webView: WKWebView, didFinish navigation: WKNavigation!) {
      print ("Load complete")
      indicator.stopAnimating ()
    }
    // Where to put

  }
  @IBAction func go (_ sender: Any) {
  }
  @IBAction func back (_ sender: Any) {
    webView.goBack ()
  }
}
  • Answer # 1

    WKNavigationDelegateis a protocol, not a class. Therefore,class ViewController: UIViewController, WKNavigationDelegate The statementdeclares thatViewControlleris a class that inheritsUIViewControllerand appliesWKNavigationDelegate. The
    The parent class and protocol are written side by side, but the meaning is actually different. (Note that an error will occur if the parent class is not written before the protocol)

    You understand about class inheritance.

    The statement

    webView.navigationDelegate = selfaskswebViewto callself.

    This tellswebViewthatselfis an object that conforms to theWKWebViewDelegateprotocol. Since the type ofwebView.navigationDelegateisWKNavigationDelegate, it can be guaranteed at compile time by assigning self.
    As a result,webViewcalls the methods of theWKNavigationDelegateprotocol implemented byself.

    TheWKNavigationDelegateprotocol is an interface that specifies howwebViewcallsself.

    You can define required methods and other methods in the protocol. If you don't provide the required ones, you can't build them. You do n’t have to provide anything you do n’t need.

    In the case of a delegate, there are many methods that are not essential, so it is convenient that you can create only what you need.

    Delegation is a design pattern. It's like an object-oriented technique, and it's called a transfer of responsibility, but in short, it is a mechanism that delegates (transfers) the actual processing (responsibility) to what will be implemented in the future. For future implementation, only the interface is defined asWKWebViewDelegateprotocol, andWKWebViewis created to use it. That way, individual users can later customize the behavior ofWKWebView.

    Note that in Swift, protocols can be applied to classes, structures, and enumerations.

    As an aside, when defining aWKNavigationDelegatemethod, I don't think thatoverrideis an interface, not a class.
    In general, interfaces are unimplemented, only types. Applying an interface to a class is not an override, because it gives a full implementation where there is only a type, and it is different from overwriting an existing implementation.
    In reality, however, the Swift protocol is an unusual interface that can have a default implementation with a protocol extension. If there is a default implementation, it's like "overriding" because it is "replaced" by the implementation of the individual class. In the meantime, I think that implicit replacement is not good, and I think it may change to add override.

  • Answer # 2

    In the meantime

      

    // When loading begins
      func webView (_ webview: WKWebView, didCommit: WKNavigation) {

    I feel that some functions such as

    are written in the viewDidLoad () function, but I think this will not work, so
    Please implement outside viewDidLoad ().

    I understand this way, is it right?

    I think the understanding is correct.
    Delegates are just names for implementation methods, and in Swift, they are realized using a mechanism called "protocol".

    and

      

    Override the methods you use

    I think that "override" is not called to implement a function written in the protocol.
    (I don't add the override keyword)

    ※ I'm not confident

Related articles