RxAlertController
- Library for Reactive programming of UIAlertController.
Features
- RxSwift Compatible
- Easy to use
Using
Import
// cocoapods
import RxAlertController_Swift
// carthage
import RxAlertController
Simple presenting
RxAlertController(title: "title", message: "message", preferredStyle: .alert)
.add(.init(title: "ok", style: .default))
.show(in: self)
.keep(by: disposeBag)
Presenting with completion
RxAlertController(title: "title", message: "message", preferredStyle: .alert)
.add(.init(title: "ok", style: .default))
.show(in: self) {
print("presenting completed")
}.keep(by: disposeBag)
Subscribe if action has id is not empty
RxAlertController(title: "title", message: "message", preferredStyle: .alert)
.add(.init(title: "cancel", style: .cancel))
.add(.init(title: "ok", id: 1, style: .default))
.show(in: self)
.subscribe(onNext: {
print("\($0.action.title) clicked")
}).disposed(by: disposeBag)
Presenting with textfields
RxAlertController(title: "title", message: "message", preferredStyle: .alert)
.add(.init(title: "cancel", style: .cancel))
.add(.init(title: "ok", id: 1, style: .default))
.addTextField {
$0.placeholder = "textfield 1"
}
.addTextField {
$0.placeholder = "textfield 2"
}
.show(in: self)
.subscribe(onNext: {
let text1 = $0.textFields?.first?.text ?? "nil"
let text2 = $0.textFields?.last?.text ?? "nil"
print("\($0.action.title) clicked -> text1: \(text1), text2: \(text2)")
}).disposed(by: disposeBag)
ActionSheet
RxAlertController(title: "title", message: "message", preferredStyle: .actionSheet)
.add(.init(title: "cancel", style: .cancel))
.add(.init(title: "1", id: 1, style: .default, userInfo: ["checked": true]))
.add(.init(title: "2", id: 2, style: .default))
.add(.init(title: "3", id: 3, style: .default))
.add(.init(title: "4", id: 4, style: .default))
.show(in: self)
.subscribe(onNext: {
print("\($0.action.title) clicked: \($0.action.id)")
}).disposed(by: disposeBag)
ActionSheet with multiple actions
let ids: [Int] = [1, 2, 3, 4]
RxAlertController(title: "title", message: "message", preferredStyle: .actionSheet)
.add(.init(title: "cancel", style: .cancel))
.add(
ids.compactMap {
let title = "\($0)"
switch $0 {
case 1:
return RxAlertAction(title: title, id: $0, style: .default, userInfo: ["checked": true])
default:
return RxAlertAction(title: title, id: $0, style: .default)
}
}
)
.show(in: self)
.subscribe(onNext: {
print("\($0.action.title) clicked: \($0.action.id)")
}).disposed(by: disposeBag)
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Installation
CocoaPods
RxAlertController is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'RxAlertController-Swift', '~> 1.1.0'
Carthage
Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.
You can install Carthage with Homebrew using the following command:
$ brew update
$ brew install carthage
To integrate RxAlertController into your Xcode project using Carthage, specify it in your Cartfile
:
github "pisces/RxAlertController" ~> 1.1.0
Run carthage update
to build the framework and drag the built RxAlertController.framework
into your Xcode project.
Requirements
iOS Deployment Target 9.0 higher
Author
Steve Kim, [email protected]
License
RxAlertController is available under the MIT license. See the LICENSE file for more info.