Skip to content

Instantly share code, notes, and snippets.

@tehillim
Created July 1, 2020 03:24
Show Gist options
  • Select an option

  • Save tehillim/b8b8429bfcc0db7849e7fc8a0763eb5f to your computer and use it in GitHub Desktop.

Select an option

Save tehillim/b8b8429bfcc0db7849e7fc8a0763eb5f to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// StateBindingExample
//
// Created by Jèwon Bong on 2020/07/01.
// Copyright © 2020 Appetizer. All rights reserved.
//
import SwiftUI
struct ContentView: View {
@State var isPlaying: Bool = false
var body: some View {
VStack {
InfoWidget(isPlaying: $isPlaying)
ControllerWidget(isPlaying: $isPlaying)
}
}
}
struct InfoWidget: View {
@Binding var isPlaying: Bool
var body: some View {
Text(self.isPlaying ? "Playing.." : "Stopped")
}
}
struct ControllerWidget: View {
@Binding var isPlaying: Bool
var body: some View {
Button(action: {
self.isPlaying.toggle()
}) {
Image(systemName: self.isPlaying ? "stop" : "playpause")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
@tehillim
Copy link
Author

tehillim commented Jul 1, 2020

SwiftUI의 State와 Binding을 이해할 수 있도록, 가독성을 위해, 간략하게 한 파일로 작성했습니다.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment