プログラミング初心者です。
・ContentViewに画面遷移後のSecondViewに設置されたSegmentedControlで選ばれた値に応じて表示させたいTextがあるのですが表示されません。
・又、一応SecondView内にもSegmentedControlで選ばれた値に応じてTextを表示させたいです。
・SecondViewからContentViewに戻る為の「Done」ボタンも表示されません。
色々なサイトを手当たり次第に読み漁り、なんとかここまではたどり着いたのですが、初心者すぎてこれ以上どうしたらいいのか分かりません。
NavigationLinkとか使えれば簡単なのかもしれませんが、何をどういじったらいいのか・・・。
ObservedObjectとか殆ど理解出来ていなくて申し訳ないのですが、宜しくお願いします。
import PlaygroundSupport import SwiftUI final class ViewModel: ObservableObject { @Published var mins = "" } struct ContentView: View { let excTime = ["05mins", "10mins", "60mins"] @State var showSecondView = false @ObservedObject var selectedIndex = ViewModel() var body: some View { VStack { Text("Exercise Time: excTime[selectedIndex]") // SecondViewのSegmentedControlで選んだ値に応じて表示させたい。(例:「05mins」) Button(action: { self.showSecondView.toggle() }) { Text("User Setting") //「User Setting」の表示およびボタン機能は問題ありません。これを押すとSecondViewに画面遷移します。 } } .sheet(isPresented: self.$showSecondView) { SecondView(isPresent: self.$showSecondView, selectedIndex: self.selectedIndex) } } } struct SecondView: View { //let excTime = ["05mins", "10mins", "60mins"] @Binding var isPresent: Bool @ObservedObject var selectedIndex: ViewModel var body: some View { NavigationView { VStack { Picker(selection: $selectedIndex.mins, label: Text("ExerciseTime")) { Text("05mins").tag(0) Text("10mins").tag(1) Text("60mins").tag(2) } .pickerStyle(SegmentedPickerStyle()) timeText(num: selectedIndex) // SegmentedControlで選んだ値に応じて表示させたい(例:「05mins」) //Text(timeText[selectedIndex.mins]) //Text("Exercise Time: timeText[selectedIndex.mins]") .navigationBarTitle("SecondView", displayMode: .inline) .navigationBarItems(trailing: Button(action: {self.isPresent = false}) { Text("Done") // SecondViewの画面右上にContentViewに戻る為の「Done」ボタンを表示させたい。 }) } } } func timeText(num:Int) -> Text { switch num { case 0: return Text("05mins") case 1: return Text("10mins") case 2: return Text("60mins") default: return Text("10mins") } } } PlaygroundPage.current.setLiveView(ContentView())
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。