Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add UITextField? #36

Closed
usinuniverse opened this issue Feb 18, 2020 · 7 comments
Closed

How to add UITextField? #36

usinuniverse opened this issue Feb 18, 2020 · 7 comments

Comments

@usinuniverse
Copy link

How do I add a UITextField?
It is not shown in the example, is there any such function?

@keisukeYamagishi
Copy link
Member

Good issues 👍
Currently there is no such function, so
I want to add this function
Do you have a good way?

@watanavex
Copy link

watanavex commented Feb 26, 2020

How about this?
(If it does n’t affect existing interfaces...)

// MARK: - UIAlertController
extension UIAlertController {    
    public func addTextInputAction(actions: [AlertAction], textFieldConfiguration: ((UITextField) -> Void)?) -> Observable<(Int, String?)> {
        return Observable.create { [weak self] observer in
            guard let self = self else { return Disposables.create() }
            var textField: UITextField?
            self.addTextField(configurationHandler: {
                textField = $0
                textFieldConfiguration?($0)
            })
            actions.map { action in
                return UIAlertAction(title: action.title, style: action.style, handler: { _ in
                    observer.onNext((action.type, textField?.text))
                    observer.onCompleted()
                })
            }.forEach(self.addAction)
            return Disposables.create { self.dismiss(animated: true) }
        }
    }
}

// MARK: - UIViewController
extension UIViewController {
    public func textInputAlert(title: String?,
                      message: String? = nil,
                      actions: [AlertAction] = [AlertAction(title: "OK")],
                      vc: UIViewController? = nil,
                      textFieldConfiguration: ((UITextField) -> Void)? = nil) -> Observable<(Int, String?)> {
        let parentVc = vc ?? self
        let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)
        return
            alertController
                .addTextInputAction(actions: actions, textFieldConfiguration: textFieldConfiguration)
                .do(onSubscribed: {
                    parentVc.present(alertController, animated: true)
                })
    }
}

In this way a single TextField can be supported.

@keisukeYamagishi
Copy link
Member

Good vibes 👍
but, I think need slightly extended interface
so I think want to same interface
And I think it's better to support multiple UITextFields instead of a single UITextField.

@watanavex
Copy link

watanavex commented Feb 27, 2020

Thank you!

I think it's better to support multiple UITextFields instead of a single UITextField.

That's true.

I feel it difficult to provide TextField in the library.
Sorry. Good ideas don't come up...

@keisukeYamagishi
Copy link
Member

I have created a PR
If you would like to, please take a look.

@watanavex
Copy link

You are amazing! That's a good idea.

@keisukeYamagishi
Copy link
Member

Thank you
Would you please review the PR if you like?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants