下記のコードについての質問が二つあります。
上二つのstructが分からない部分なのですが、3つ目のstructが無いと実行できないので載せています。
今回行いたいのは、カレンダーの下にある+ボタンを押して「タイトル」のTextFieldに文字を入力する。
そして、Enterキーを押した後に保存ボタンを押す。
すると、カレンダーの下にその「タイトル」が表示される。
これを、日毎に行えるようにしたいです。(日毎に自ら入力した別々のタイトルが表示されるという意味です。)
①struct CalendarPage で宣言している @State var array : [Date] = [] と @State var arraytitle : [String] = [] を再起動しても保存されたままにしたいです。@AppStorageと書くとエラーが出たので、userDefaultsでも書いたのですがViewの中だから不可能だとエラーが出ました。
どのような仕様にすれば良いでしょうか?
②初回のタイトル保存は出来るのですが、別の日を選択して行おうとするとTextFieldに入力はできるものの、カレンダーの下に表示されません。
struct SecondView にある @State var new = "" を毎回初期化が必要なのかとも考えましたが、その方法も分かりません。
どうすれば、その日毎に入力したタイトルを表示出来るのでしょうか?
質問が2つもありお手数をおかけしますが、よろしくお願いします。
swift
1import FSCalendar 2import CalculateCalendarLogic 3 4struct CalendarPage:View{ 5 @State private var show: Bool = false 6 @State var selectedDate = Date() 7 @State var array : [Date] = [] 8 @State var arraytitle : [String] = [] 9 var body: some View{ 10 VStack{ 11 Calendarpage(selectedDate:$selectedDate).frame(height:360) 12 HStack{ 13 Text(selectedDate,style:.date).padding() 14 Spacer() 15 Button(action:{self.show = true}){ 16 Text("+").font(.largeTitle).frame(width:50) 17 }.sheet(isPresented:self.$show){ 18 SecondView(show:$show,selectedDate:$selectedDate,array:$array,arraytitle:$arraytitle) 19 } 20 } 21 if(array.count == 0){ 22 } 23 else{ 24 ForEach(0..<array.count){index in 25 if(array[index] == selectedDate){ 26 Text("\(arraytitle[index])") 27 } 28 } 29 } 30 Spacer() 31 } 32 } 33} 34struct SecondView: View { 35 @Binding var show:Bool 36 @Binding var selectedDate:Date 37 @Binding var array:[Date] 38 @Binding var arraytitle:[String] 39 @State var new = "" 40 var body: some View { 41 VStack { 42 HStack{ 43 Spacer() 44 Button(action:{array.append(selectedDate);self.show.toggle()}){Text("保存")}.frame(width:75,height:75) 45 } 46 HStack{ 47 Text("タイトル") 48 TextField("",text:$new,onCommit:{arraytitle.append(new)}).overlay(RoundedRectangle(cornerRadius:1).stroke(Color.black, lineWidth:1)) 49 } 50 Spacer() 51 } 52 } 53} 54 55struct Calendarpage: UIViewRepresentable { 56 @Binding var selectedDate:Date 57 58 59 func makeUIView(context: Context) -> UIView { 60 typealias UIViewType = FSCalendar 61 let fsCalendar = FSCalendar() 62 fsCalendar.delegate = context.coordinator 63 fsCalendar.dataSource = context.coordinator 64 fsCalendar.appearance.headerTitleFont = UIFont.systemFont(ofSize: 20) 65 fsCalendar.appearance.weekdayFont = UIFont.systemFont(ofSize: 20) 66 fsCalendar.appearance.titleFont = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold) 67 fsCalendar.appearance.headerDateFormat = "yyyy/MM" 68 fsCalendar.appearance.todayColor = .gray 69 fsCalendar.appearance.selectionColor = .clear 70 fsCalendar.appearance.borderSelectionColor = .blue 71 fsCalendar.appearance.titleSelectionColor = .black 72 fsCalendar.appearance.borderRadius = 0 73 74 75 return fsCalendar 76 } 77 78 79 func updateUIView(_ uiView: UIView, context: Context) { 80 } 81 82 83 func makeCoordinator() -> Coordinator { 84 return Coordinator(self) 85 } 86 87 88 class Coordinator: NSObject, FSCalendarDelegateAppearance, FSCalendarDataSource{ 89 var parent : Calendarpage 90 91 92 init(_ parent: Calendarpage) { 93 self.parent = parent 94 } 95 96 97 func calendar(_ calendar:FSCalendar, didSelect date:Date, at monthPosition:FSCalendarMonthPosition){ 98 parent.selectedDate = date 99 } 100 101 102 func judgeHoliday(_ date : Date) -> Bool { // 祝日判定を行い結果を返すメソッド(True:祝日) 103 let tmpCalendar = Calendar(identifier: .gregorian) //祝日判定用のカレンダークラスのインスタンス 104 let year = tmpCalendar.component(.year, from: date) //祝日判定を行う年を取得 105 let month = tmpCalendar.component(.month, from: date) //祝日判定を行う月を取得 106 let day = tmpCalendar.component(.day, from: date) //祝日判定を行う日を取得 107 let holiday = CalculateCalendarLogic() //祝日判定のインスタンスの生成 108 return holiday.judgeJapaneseHoliday(year: year, month: month, day: day) 109 } 110 111 func getDay(_ date:Date) -> (Int,Int,Int){ //年月日をIntで取得 112 let tmpCalendar = Calendar(identifier: .gregorian) 113 let year = tmpCalendar.component(.year, from: date) 114 let month = tmpCalendar.component(.month, from: date) 115 let day = tmpCalendar.component(.day, from: date) 116 return (year,month,day) 117 } 118 119 120 func getWeekIdx(_ date: Date) -> Int{ //曜日判定(日曜日:1 〜 土曜日:7) 121 let tmpCalendar = Calendar(identifier: .gregorian) 122 return tmpCalendar.component(.weekday, from: date) 123 } 124 125 126 func calendar(_ calendar: FSCalendar, appearance: FSCalendarAppearance, titleDefaultColorFor date: Date) -> UIColor? { //土日や祝日の日の文字色を変える 127 if self.judgeHoliday(date){ //祝日判定をする(祝日は赤色で表示する) 128 return UIColor.red 129 } 130 let weekday = self.getWeekIdx(date) //土日の判定を行う(土曜日は青色、日曜日は赤色で表示する) 131 if weekday == 1 { //日曜日 132 return UIColor.red 133 } 134 else if weekday == 7 { //土曜日 135 return UIColor.blue 136 } 137 return nil 138 } 139 } 140}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/06/05 04:40