Skip to content

Instantly share code, notes, and snippets.

@nsakaimbo
Last active July 21, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nsakaimbo/f85404b6213f7f6e9486b78550a4cd1d to your computer and use it in GitHub Desktop.
Save nsakaimbo/f85404b6213f7f6e9486b78550a4cd1d to your computer and use it in GitHub Desktop.
Eureka - Adding an Option to a PushRow
import Eureka
import UIKit
// This particular example assumes the view controller is embedded in a navigation controller
// (but the option can be added any number of ways)
class ViewController: FormViewController {
var options = ["first", "second"]
var pushRow: PushRow<String>! {
return form.rowBy(tag: "PushRow") as! PushRow<String>
}
override func viewDidLoad() {
super.viewDidLoad()
let button = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(presentAlertWithTextField(_:)))
navigationItem.rightBarButtonItem = button
// Form
let section = Section()
section <<< PushRow<String>("PushRow") {
$0.title = "Options"
$0.cellUpdate { cell, row in
row.options = self.options
}
}
form +++ section
}
func presentAlertWithTextField(_ sender: Any) {
let alert = UIAlertController(title: "Add Option", message: "Enter Option Below", preferredStyle: .alert)
alert.addTextField()
let alertAction = UIAlertAction(title: "Add to Push Row", style: .default) { [weak self] action in
guard let sself = self,
let textField = alert.textFields?[0],
let text = textField.text else {
return
}
sself.options.append(text)
sself.pushRow.updateCell()
}
alert.addAction(alertAction)
navigationController?.present(alert, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment