Coder Social home page Coder Social logo

chandlerdea / responderchaindemo Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 7 KB

Using the responder chain to deliver UIControl events to view controllers

Swift 100.00%
swift-4 responder-chain uitableviewcell uitableview uikit ios macos cocoa-touch cocoa

responderchaindemo's Introduction

ResponderChainDemo

This project is meant to demonstrate using the responder chain to delegate the actions of UIControl objects inside of a UITableViewCell. This also works with UICollectionViewCells, but this project will use UITableViewCells. This project is simply meant to demonstrate one approach to a common situation that iOS developers encounter.

The Problem

Let's say we have a UITableVewCell subclass called SomeCell, which has a button. We want the view controller which owns the table view to be notified when the button inside SomeCell is pressed.

The Old Way

  1. Create a delegate protocol SomeCellDelegate
  2. Add a delegate property of type SomeCellDelegate to the cell
  3. Make the cell the target of the button
  4. Inside of the button's target method, call the correct function on the delegate, usually passing the cell as an argument
  5. Make sure the view controller conforms to the delegate

This isn't necessarily bad, but there are some potential issues.

  • You could easily have a retain cycle if the protocol isn't a class protocol, and the delegate property isn't weak
  • You could also have to manually set the delegate of the cell

The Responder Chain Way

  1. Create a delegate protocol SomeCellDelegate. Make sure that the protocol is marked with @objc, because we need to use the Objective-C runtime

  2. Make the cell the target of the button

  3. Inside of the button's target method, add this line:

     UIApplication.shared.sendAction(#selector(SomeCellDelegate.doSuff(_:)), to: .none, from: self, for: .none)
    
  4. Make sure the view controller conforms to the delegate

The sendAction funtion will cause the system to go up the responder chain, and call resonds(to:) on each responder, until the function returns true. This solves the potential retain cycle issue, and is overall a more clean solution, using the responder chain as it is meant to be used.

responderchaindemo's People

Contributors

chandlerdea avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.