How does one go about making a delegate, i.e. NSUserNotificationCenterDelegate
in swift?
相关问题
- “Zero out” sensitive String data in Swift
- SwiftUI: UIImage (QRCode) does not load after call
- Get the NSRange for the visible text after scroll
- UIPanGestureRecognizer is not working in iOS 13
- What does a Firebase observer actually do?
相关文章
- Using if let syntax in switch statement
- Enum with associated value conforming to CaseItera
- Swift - hide pickerView after value selected
- Is there a Github markdown language identifier for
- How can I vertically align my status bar item text
- Adding TapGestureRecognizer to UILabel in Swift
- Attempt to present UIAlertController on View Contr
- Swift - Snapshotting a view that has not been rend
It is not that different from obj-c. First, you have to specify the protocol in your class declaration, like following:
The implementation will look like following:
Of course, you have to set the delegate. For example:
Delegates are a design pattern that allows one object to send messages to another object when a specific event happens. Imagine an object A calls an object B to perform an action. Once the action is complete, object A should know that B has completed the task and take necessary action, this can be achieved with the help of delegates! Here is a tutorial implementing delegates step by step in swift 3
Tutorial Link
Here's a little help on delegates between two view controllers:
Step 1: Make a protocol in the UIViewController that you will be removing/will be sending the data.
Step2: Declare the delegate in the sending class (i.e. UIViewcontroller)
Step3: Use the delegate in a class method to send the data to the receiving method, which is any method that adopts the protocol.
Step 4: Adopt the protocol in the receiving class
Step 5: Implement the delegate method
Step 6: Set the delegate in the prepareForSegue:
And that should work. This is of course just code fragments, but should give you the idea. For a long explanation of this code you can go over to my blog entry here:
segues and delegates
If you are interested in what's going on under the hood with a delegate I did write on that here:
under the hood with delegates
Very easy step by step (100% working and tested)
step1: Create method on first view controller
step2: Set delegate while push to second view controller
step3: set delegate like
class ViewController: UIViewController,ProcessStatusDelegate {
step4: Create protocol
step5: take a variable
step6: While go back to previous view controller call delegate method so first view controller notify with data
First class:
Second class:
Simple Example: