Delegate is convenient to use when you want to reuse parts

I finally found out about the delegate, so I made a memorandum.

It is convenient to use when you want to use small parts such as buttons, but want to process differently for each page.

This time, I will use the buttons below. From the left, "Back button", "Commentary button", and "Next button". I want to use this button class on various pages.

デリゲート.png

[Thing you want to do] I want to switch the button press action for each page. When you press the back button, one page transitions to the previous screen and another page ends modal. Or for other pages, I want to save the page before closing the page. etc

[Solutions] Write a button press action on each page. The design part of the button may be shared, but the function of the action part is delegated to each page. This is the role of the delegate, which translates as "transfer."

[manner]

  1. Write the protocol in the View of the button part. If you want to use the button of footerView class, promise to implement it using the fixed method name.
FooterView.swift
protocol TapLeftButtonVCDelegate {
    func tapLeftButton()
}
protocol TapCenterButtonVCDelegate {
    func tapCenterButton()
}
protocol TapRightButtonVCDelegate {
    func tapRightButton()
}
  1. On each page, instantiate the button class and declare that you are the delegate of the button action.
ViewController.swift
    //Since I want to use only the right button on this page, I declare only the protocol for the right button.
    @IBOutlet weak var footerView: FooterView!
    override func viewDidLoad() {
        super.viewDidLoad()
        footerView.tapRightButtonVCDelegate = self
    }
  1. Implement the action after pressing the button on each page.
extension ViewController: TapRightButtonVCDelegate {
    func tapRightButton() {
        //Proceed to the next screen, save process, write anything you like
    }
}
  1. Finally, return to the Button's FooterView and instruct to process the delegate destination when the button is pressed.
class FooterView: UIView {
    var tapRightButtonVCDelegate: TapRightButtonVCDelegate?

    @IBAction func tapRightButton(_ sender: Any) {
        tapRightButtonVCDelegate?.tapRightButton()
    }
}

Now you can reuse the button on various pages.

[Impression] It was how to use the delegate considering the purpose. There may be other convenient ways to use it.

Recommended Posts

Delegate is convenient to use when you want to reuse parts
ProxyFactory is convenient when you want to test AOP in Spring!
When you want to use the method outside
If you want to make a Java application a Docker image, it is convenient to use jib.
Practice to use when you want to execute different processing groups serially
When you want to bind InputStream in JDBI3
[RSpec] When you want to use the instance variable of the controller in the test [assigns is not recommended]
[Swift] Use nonzeroBitCount when you want popcnt in Swift
What do you use when converting to String?
[Ruby] When you want to replace multiple characters
What to do when you want to delete a migration file that is "NO FILE"
I want to use Clojure's convenient functions in Kotlin
If you want to use Mockito with Kotlin, use mockito-kotlin
When you want to dynamically replace Annotation in Java8
JAWJAW is convenient if you use WordNet from Java
A memo to check when you try to use Lombok
Code to use when you want to process Json with only standard library in Java
Use JLine when you want to handle keystrokes on the console character by character in Java
When you want to explicitly write OR or AND with ransack
When you want to change the MySQL password of docker-compose
What to do when you want to know the source position where the method is defined in binding.pry
When you want to ZIP download the image data saved locally
When you have introduced devise but want to add more columns
Function is very easy to use
I want to use FormObject well
A memo when you want to clear the time part of the calendar
If you want to use Oracle JDK 11 from September (add Amazon Corretto)
When you want Rails to disable a session for a specific controller only
[JPA] Personal memo Paging processing is very convenient if you use Pageable
eclipse I definitely want you to use Transcendental Recommended Shortcut Key (Windows)
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
I want you to use Enum # name () for the Key of SharedPreference
ActiveModel is convenient when creating Form class
I want to use DBViewer with Eclipse 2018-12! !!
Error when trying to use heroku command
Basic Rails commands you want to learn
I want to use @Autowired in Servlet
[Rails] Enum is easier to use! Enumelize!
When is it said that you can use try with a Swift error?
You may not want to use the remove method in ArrayList very often
[Java] Why do you bother to use the interface (Spring is also available)
When you want to notify an error somewhere when using graphql-spring-boot in Spring Boot
I want you to use Scala as Better Java for the time being
[Spring Boot] If you use Spring Boot, it was convenient to use a lot of util.