クラス、メンバー、プロトコル、継承、クラス拡張などを知る
ShareData.swift
class ShareData: ObservableObject { @Published var isOn = false @Published var num = 1 }
SettingView.swift
struct SettingView: View { @EnvironmentObject var setData: ShareData @Binding var isPresented: Bool var body: some View { NavigationView { VStack { Toggle(isOn: $setData.isOn){ Text("設定: \(setData.isOn ? "ON": "OFF")") }.frame(width: 250) Stepper(value: $setData.num, in: 1...5){ Text("★ :\(setData.num)") } .frame(width: 250) .font(.title2) .frame(maxWidth: .infinity, maxHeight: .infinity) .background(Color(red: 0.9, green: 0.9, blue: 0.5)) .toolbar { ToolbarItem(placement: .navigationBarTrailing){ Button("閉じる"){ isPresented = false } } } } } } } struct SettingView_Previews: PreviewProvider { static var previews: some View { SettingView(isPresented: Binding.constant(false)) .environmentObject(ShareData()) } }
ContentView.swift
struct ContentView: View { @EnvironmentObject var setData: ShareData @State var isShow: Bool = false var body: some View { VStack{ GroupBox(label: Label("設定", systemImage: "gearshape")){ Text("\(setData.isOn ? "ON" : "OFF")") if setData.isOn { Text(String(repeating: "★", count: setData.num)) } }.frame(width: 300) Button (action: { isShow = true }) { Label("設定を変える", systemImage: "ellipsis.circle") } .padding() .sheet(isPresented: $isShow){ SettingView(isPresented: $isShow) } } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() .environmentObject(ShareData()) } }
@main struct LinkURLSampleApp: App { var body: some Scene { WindowGroup { ContentView().environmentObject(ShareData()) } } }
なんかよくわからんくなってきた。
かなり修行が必要やな