mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
UIKit demo app: use @IBSegueAction
This commit is contained in:
1 parent
dbb19979d6
commit
66bbf15f58
3 files changed
+47
-60
No files matched your search
@@ -12,13 +12,13 @@
|
||||
<objects>
|
||||
<tableViewController id="iNr-Oe-muq" customClass="PlayerEditionViewController" customModule="GRDBDemoiOS" customModuleProvider="target" sceneMemberID="viewController">
|
||||
<tableView key="view" clipsSubviews="YES" contentMode="scaleToFill" dataMode="static" style="insetGrouped" separatorStyle="none" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="-1" estimatedSectionHeaderHeight="-1" sectionFooterHeight="-1" estimatedSectionFooterHeight="-1" id="vlV-lc-eQA">
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
|
||||
<rect key="frame" x="0.0" y="0.0" width="414" height="842"/>
|
||||
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
|
||||
<sections>
|
||||
<tableViewSection headerTitle="Name" id="Nzq-eO-jT3">
|
||||
<cells>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" id="2gb-gB-918">
|
||||
<rect key="frame" x="20" y="32" width="374" height="44"/>
|
||||
<rect key="frame" x="20" y="49.5" width="374" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="2gb-gB-918" id="iyM-TJ-JB4">
|
||||
<rect key="frame" x="0.0" y="0.0" width="374" height="44"/>
|
||||
@@ -46,7 +46,7 @@
|
||||
<tableViewSection headerTitle="Score" id="Urb-uF-sj1">
|
||||
<cells>
|
||||
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" selectionStyle="none" indentationWidth="10" id="vp3-Ft-Gth">
|
||||
<rect key="frame" x="20" y="125.5" width="374" height="44"/>
|
||||
<rect key="frame" x="20" y="143" width="374" height="44"/>
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
<tableViewCellContentView key="contentView" opaque="NO" clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="center" tableViewCell="vp3-Ft-Gth" id="ccO-0O-u72">
|
||||
<rect key="frame" x="0.0" y="0.0" width="374" height="44"/>
|
||||
@@ -113,7 +113,7 @@
|
||||
<autoresizingMask key="autoresizingMask"/>
|
||||
</navigationBar>
|
||||
<connections>
|
||||
<segue destination="iNr-Oe-muq" kind="relationship" relationship="rootViewController" id="e5b-3U-9Vz"/>
|
||||
<segue destination="iNr-Oe-muq" kind="relationship" relationship="rootViewController" destinationCreationSelector="makePlayerCreationViewController:" id="e5b-3U-9Vz"/>
|
||||
</connections>
|
||||
</navigationController>
|
||||
<placeholder placeholderIdentifier="IBFirstResponder" id="kNr-vk-I5l" userLabel="First Responder" sceneMemberID="firstResponder"/>
|
||||
@@ -168,7 +168,7 @@
|
||||
</subviews>
|
||||
</tableViewCellContentView>
|
||||
<connections>
|
||||
<segue destination="iNr-Oe-muq" kind="show" identifier="Edit" id="Lgu-Yd-HSy"/>
|
||||
<segue destination="iNr-Oe-muq" kind="show" identifier="" destinationCreationSelector="makePlayerEditionViewController:" id="Lgu-Yd-HSy"/>
|
||||
</connections>
|
||||
</tableViewCell>
|
||||
</prototypes>
|
||||
@@ -183,7 +183,7 @@
|
||||
<navigationItem key="navigationItem" title="Hall Of Fame" id="QIv-M6-kT1">
|
||||
<barButtonItem key="leftBarButtonItem" systemItem="add" id="2bK-Fl-55Q">
|
||||
<connections>
|
||||
<segue destination="ein-Sr-i8w" kind="presentation" identifier="New" id="ehP-ZN-Nt4"/>
|
||||
<segue destination="ein-Sr-i8w" kind="presentation" identifier="" id="ehP-ZN-Nt4"/>
|
||||
</connections>
|
||||
</barButtonItem>
|
||||
</navigationItem>
|
||||
@@ -198,7 +198,7 @@
|
||||
</scene>
|
||||
</scenes>
|
||||
<inferredMetricsTieBreakers>
|
||||
<segue reference="Lgu-Yd-HSy"/>
|
||||
<segue reference="e5b-3U-9Vz"/>
|
||||
</inferredMetricsTieBreakers>
|
||||
<resources>
|
||||
<systemColor name="secondaryLabelColor">
|
||||
|
||||
+31
-34
@@ -1,27 +1,19 @@
|
||||
import UIKit
|
||||
|
||||
class PlayerEditionViewController: UITableViewController {
|
||||
enum Presentation {
|
||||
/// Modal presentation: edition ends with the "Commit" unwind segue.
|
||||
case modal
|
||||
enum Mode {
|
||||
/// Edition ends with the "Commit" unwind segue.
|
||||
case creation
|
||||
|
||||
/// Push presentation: edition ends when user hits the back button.
|
||||
case push
|
||||
/// Edition ends when user hits the back button.
|
||||
case edition
|
||||
}
|
||||
|
||||
/// The edited player
|
||||
var player: Player! {
|
||||
didSet {
|
||||
configureForm()
|
||||
}
|
||||
}
|
||||
private(set) var player: Player
|
||||
|
||||
/// The presentation mode
|
||||
var presentation: Presentation! {
|
||||
didSet {
|
||||
configureNavigationItem()
|
||||
}
|
||||
}
|
||||
let mode: Mode
|
||||
|
||||
@IBOutlet private weak var cancelBarButtonItem: UIBarButtonItem!
|
||||
@IBOutlet private weak var commitBarButtonItem: UIBarButtonItem!
|
||||
@@ -30,10 +22,21 @@ class PlayerEditionViewController: UITableViewController {
|
||||
@IBOutlet private weak var scoreCell: UITableViewCell!
|
||||
@IBOutlet private weak var scoreTextField: UITextField!
|
||||
|
||||
init?(_ coder: NSCoder, mode: Mode, player: Player) {
|
||||
self.mode = mode
|
||||
self.player = player
|
||||
super.init(coder: coder)
|
||||
}
|
||||
|
||||
@available(*, unavailable)
|
||||
required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
configureForm()
|
||||
configureNavigationItem()
|
||||
configureForm()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,24 +58,22 @@ extension PlayerEditionViewController {
|
||||
override func willMove(toParent parent: UIViewController?) {
|
||||
super.willMove(toParent: parent)
|
||||
|
||||
if case .push = presentation, parent == nil {
|
||||
if mode == .edition, parent == nil {
|
||||
// Self is popping from its navigation controller
|
||||
saveChanges()
|
||||
}
|
||||
}
|
||||
|
||||
private func configureNavigationItem() {
|
||||
guard isViewLoaded else { return }
|
||||
|
||||
if let presentation = presentation {
|
||||
switch presentation {
|
||||
case .modal:
|
||||
navigationItem.leftBarButtonItem = cancelBarButtonItem
|
||||
navigationItem.rightBarButtonItem = commitBarButtonItem
|
||||
case .push:
|
||||
navigationItem.leftBarButtonItem = nil
|
||||
navigationItem.rightBarButtonItem = nil
|
||||
}
|
||||
switch mode {
|
||||
case .creation:
|
||||
navigationItem.title = "New Player"
|
||||
navigationItem.leftBarButtonItem = cancelBarButtonItem
|
||||
navigationItem.rightBarButtonItem = commitBarButtonItem
|
||||
case .edition:
|
||||
navigationItem.title = player.name
|
||||
navigationItem.leftBarButtonItem = nil
|
||||
navigationItem.rightBarButtonItem = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -104,14 +105,12 @@ extension PlayerEditionViewController: UITextFieldDelegate {
|
||||
|
||||
@IBAction func textFieldDidChange(_ textField: UITextField) {
|
||||
// User has edited the player: prevent interactive dismissal
|
||||
if case .modal = presentation {
|
||||
if case .creation = mode {
|
||||
isModalInPresentation = true
|
||||
}
|
||||
}
|
||||
|
||||
private func configureForm() {
|
||||
guard isViewLoaded else { return }
|
||||
|
||||
nameTextField.text = player.name
|
||||
|
||||
if player.score == 0 && player.id == nil {
|
||||
@@ -122,9 +121,7 @@ extension PlayerEditionViewController: UITextFieldDelegate {
|
||||
}
|
||||
|
||||
private func saveChanges() {
|
||||
guard var player = self.player else {
|
||||
return
|
||||
}
|
||||
var player = self.player
|
||||
player.name = nameTextField.text ?? ""
|
||||
player.score = scoreTextField.text.flatMap { Int($0) } ?? 0
|
||||
try! AppDatabase.shared.savePlayer(&player)
|
||||
|
||||
+9
-19
@@ -126,26 +126,16 @@ class PlayerListViewController: UITableViewController {
|
||||
// MARK: - Navigation
|
||||
|
||||
extension PlayerListViewController {
|
||||
@IBSegueAction func makePlayerEditionViewController(_ coder: NSCoder) -> PlayerEditionViewController? {
|
||||
guard let indexPath = tableView.indexPathForSelectedRow,
|
||||
let player = dataSource.itemIdentifier(for: indexPath)
|
||||
else { return nil }
|
||||
return PlayerEditionViewController(coder, mode: .edition, player: player)
|
||||
}
|
||||
|
||||
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
|
||||
if segue.identifier == "Edit" {
|
||||
guard let controller = segue.destination as? PlayerEditionViewController,
|
||||
let indexPath = tableView.indexPathForSelectedRow,
|
||||
let player = dataSource.itemIdentifier(for: indexPath)
|
||||
else { return }
|
||||
controller.title = player.name
|
||||
controller.player = player
|
||||
controller.presentation = .push
|
||||
}
|
||||
else if segue.identifier == "New" {
|
||||
guard let navigationController = segue.destination as? UINavigationController,
|
||||
let controller = navigationController.viewControllers.first as? PlayerEditionViewController
|
||||
else { return }
|
||||
setEditing(false, animated: true)
|
||||
controller.title = "New Player"
|
||||
controller.player = Player(id: nil, name: "", score: 0)
|
||||
controller.presentation = .modal
|
||||
}
|
||||
@IBSegueAction func makePlayerCreationViewController(_ coder: NSCoder) -> PlayerEditionViewController? {
|
||||
let player = Player(id: nil, name: "", score: 0)
|
||||
return PlayerEditionViewController(coder, mode: .creation, player: player)
|
||||
}
|
||||
|
||||
@IBAction func cancelPlayerEdition(_ segue: UIStoryboardSegue) {
|
||||
|
||||
Reference in new issue
Block a user