Swiftpack.co - Package - Amzd/ResponderChain
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.

⛓️ ResponderChain

Cross-platform first responder handling without subclassing views or making custom ViewRepresentables in SwiftUI

Features

  1. Getting the current first responder through ResponderChain.firstResponder
  2. Setting a new first responder through ResponderChain.firstResponder
  3. Getting tagged views available for receiveing first responder through ResponderChain.availableResponders

Overview

Attach the ResponderChain as environmentObject.

// In the SceneDelegate or ApplicationDelegate where you have access to the window:
let rootView = Example().environmentObject(ResponderChain(forWindow: window))

// SwiftUI only:
Example().withResponderChainForCurrentWindow()

Tag views that can become first responder.

TextField(...).responderTag("MyTextField")

Check tagged views that are currently available to become first responder.

chain.availableResponders.contains("MyList")

Make tagged views become first responder.

chain.firstResponder = "MyTextField"
if chain.firstResponder == nil {
    print("Failed")
}

This is completely safe, if "MyTextField" was either not available to become first responder or it wasn't tagged properly; chain.firstResponder will become nil

Resign first responder.

chain.firstResponder = nil

Example

Attach the ResponderChain as environmentObject.

...
// In the SceneDelegate or ApplicationDelegate where you have access to the window:
let rootView = ResponderChainExample().environmentObject(ResponderChain(forWindow: window))

// SwiftUI only:
ResponderChainExample().withResponderChainForCurrentWindow()
...

ResponderChainExample.swift

struct ResponderChainExample: View {
    @EnvironmentObject var chain: ResponderChain
    
    var body: some View {
        VStack(spacing: 20) {
            // Show which view is first responder
            Text("Selected field: \(chain.firstResponder?.description ?? "Nothing selected")")
            
            // Some views that can become first responder
            TextField("0", text: .constant(""), onCommit: { chain.firstResponder = "1" }).responderTag("0")
            TextField("1", text: .constant(""), onCommit: { chain.firstResponder = "2" }).responderTag("1")
            TextField("2", text: .constant(""), onCommit: { chain.firstResponder = "3" }).responderTag("2")
            TextField("3", text: .constant(""), onCommit: { chain.firstResponder = nil }).responderTag("3")
            
            // Buttons to change first responder
            HStack {
                Button("Select 0", action: { chain.firstResponder = "0" })
                Button("Select 1", action: { chain.firstResponder = "1" })
                Button("Select 2", action: { chain.firstResponder = "2" })
                Button("Select 3", action: { chain.firstResponder = "3" })
            }
        }
        .padding()
        .onAppear {
            // Set first responder on appear
            DispatchQueue.main.async {
                chain.firstResponder = "0"
            }
        }
    }
}

Github

link
Stars: 6

Releases

Fixed crash with Alert - 2021-01-19T12:50:24

Can now resign firstResponder by setting it to nil - 2021-01-06T18:14:04

Introduced method swizzling to get updates on the firstResponder changing on iOS.

SwiftUI only support - 2020-12-31T19:17:05

Added View.withResponderChainForCurrentWindow() so you can use ResponderChain in SwiftUI only apps

initial release - 2020-12-08T10:54:49