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 ~ didcommit
andfunc webView ~ didfinish
used when the screen starts to load are considered to be delegate methods. This delegate method is basically aclass ViewController: UIViewController, WKNavigationDelegate
when 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 = self
It must be specified whether to process in the class (in this case, since self is specified,func webView
must 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
-
Answer # 2
In the meantime
// When loading begins
func webView (_ webview: WKWebView, didCommit: WKNavigation) {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
- i would like to know if there is a download method for old php (5640) for windows
- ios - [swift] there is a code that i do not understand what is being processed by firebase login
- ios - i want to execute a method that uses both ui widgets connected to different classes
- i don't understand graphql's query with predicates in aws appsync (i'm using ios swift)
- ios - how to learn to understand synchronous processing and asynchronous processing
- ios - i am in trouble because i do not understand the variables to write in the preview
- c ++ - i would like to know the implementation method when the definition and the declaration are separated by the implementatio
- i would like to know the api specifications (acquisition/output method etc)
- i don't understand the syntax of a method name in python
- ios - swift when you do not want to execute the superclass method in the subclass
- ios - how to use viewcontroller in common method
- Deeply understand the extension method in C #
- Use Spring Boot to simply understand the configuration method of Druid's monitoring system
- i don't understand how to use javascript assign method
- ios - how to execute nstimer method on the application side from watchos2?
- python - i would like to know the meaning of bound method miscmainloop of
- ios - i want to execute an arbitrary method after the processing of the delegated method is completed
- python - i would like to discuss a method for detecting circles in multiple images
- ios - i want to call the urlsession class from the searchbuttonclicked method
- i would like to know how to add a new method to an existing method in javascript
- ios - i want to simplify the code for visualizing the swift game hp
- ios - i can't press a button created with a custom cell
- ios - i don't know how to delete values stored in userdefaults
- ios - swift cell label
- [swift/ios] system sound doesn't keep sounding
- ios - how to display the data acquired by api in complex json format
- ios - questions about masterdetail
- [swift/ios] i want to remove the check that was added at the time of transition on the sound selection screen when selecting ano
- ios - how to specify the return value of a swift function
- ios - [swift/cloudkit] how to add from code to string (list) type field of record
WKNavigationDelegate
is a protocol, not a class. Therefore,class ViewController: UIViewController, WKNavigationDelegate The statement
declares thatViewController
is a class that inheritsUIViewController
and appliesWKNavigationDelegate
. TheThe 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 statementwebView.navigationDelegate = self
askswebView
to callself
.This tells
webView
thatself
is an object that conforms to theWKWebViewDelegate
protocol. Since the type ofwebView.navigationDelegate
isWKNavigationDelegate
, it can be guaranteed at compile time by assigning self.As a result,
webView
calls the methods of theWKNavigationDelegate
protocol implemented byself
.The
WKNavigationDelegate
protocol is an interface that specifies howwebView
callsself
.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 as
WKWebViewDelegate
protocol, andWKWebView
is 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 a
WKNavigationDelegate
method, I don't think thatoverride
is 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.