Created
February 25, 2020 18:39
-
-
Save drewmccormack/09e47ff44572c23015d29dbce8e0fb96 to your computer and use it in GitHub Desktop.
Simple example of MMVM with SwiftUI.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import SwiftUI | |
import Combine | |
let dateFormatter: DateFormatter = { | |
let dateFormatter = DateFormatter() | |
dateFormatter.locale = Locale(identifier: "en_US_POSIX") | |
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" | |
return dateFormatter | |
}() | |
class MainController: ObservableObject { | |
static let shared = MainController() | |
@Published var contentViewModel: ContentView.Model = .init() | |
} | |
struct ContentView: View { | |
class Model: ObservableObject { | |
@Published var dateString: String = dateFormatter.string(from: Date()) | |
} | |
@ObservedObject var model: Model | |
var body: some View { | |
VStack { | |
Text(model.dateString) | |
Button(action: { | |
self.model.dateString = dateFormatter.string(from: Date()) | |
}, label: { Text("Set Date") }) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment