Skip to content

Instantly share code, notes, and snippets.

@RuiNelson
Created July 24, 2020 02:28
Show Gist options
  • Save RuiNelson/168d9230b1e6791b4879e183db3bb003 to your computer and use it in GitHub Desktop.
Save RuiNelson/168d9230b1e6791b4879e183db3bb003 to your computer and use it in GitHub Desktop.
import UIKit
import SwiftUI
class ViewController: UIViewController {
var hosting: UIHostingController<SwiftUIView>?
override func viewDidLoad() {
super.viewDidLoad()
let rootView = SwiftUIView(delegate: self, toggleStatus: false, stepper: 18)
self.hosting = UIHostingController(rootView: rootView)
self.addChild(hosting!)
hosting!.view.frame = self.view.frame
self.view.addSubview(hosting!.view)
}
func buttonPressed(toggleStatus: Bool) {
let message = toggleStatus ? "on" : "off"
let alert = UIAlertController(title: "hello", message: "The toggle is \(message)", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default) { (_) in
alert.dismiss(animated: true,
completion: nil)
}
alert.addAction(ok)
self.present(alert, animated: true, completion: nil)
}
}
struct SwiftUIView: View {
weak var delegate: ViewController?
@State var toggleStatus = false
@State var stepper = 0
func change() {
self.toggleStatus = true
self.stepper = 18
print("change")
}
func checkup() -> Bool {
return stepper <= 25 && toggleStatus == true
}
var body: some View {
VStack {
Text("Hello")
Text("World")
Spacer()
Toggle(isOn: $toggleStatus, label: { Text("Toggle") })
Stepper(value: $stepper, in: 0...999, label: { Text("\(stepper) years old") })
Spacer()
Button(action: {
self.delegate?.buttonPressed(toggleStatus: self.toggleStatus)
}) {
Text("Press Me")
}.disabled(checkup() == false)
}
.padding(.horizontal)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment