回答編集履歴

7

ZStack を忘れていたため追記しました。

2023/01/07 02:54

投稿

uni2
uni2

スコア254

test CHANGED
@@ -61,6 +61,7 @@
61
61
  @State var isPresentedProgressView = false
62
62
  @State var isNavigationLinkActive: [Bool] = []
63
63
  var body: some View {
64
+ ZStack{
64
65
  NavigationView {
65
66
  List {
66
67
  ForEach(0 ..< threads.count, id: \.self) { i in
@@ -116,6 +117,7 @@
116
117
  ProgressView("読み込み中")
117
118
  }
118
119
  }
120
+ }
119
121
 
120
122
  private func manageProgress(_ index: Int) {
121
123
  // ProgressView 表示

6

インジケーターを忘れていたため追記しました。

2023/01/07 02:52

投稿

uni2
uni2

スコア254

test CHANGED
@@ -111,6 +111,10 @@
111
111
  .alert(isPresented: $showAlert, content: {
112
112
  Alert(title: Text("削除に失敗しました。権限がないようです。"))
113
113
  })
114
+ if isPresentedProgressView {
115
+ Color.gray.opacity(0.5)
116
+ ProgressView("読み込み中")
117
+ }
114
118
  }
115
119
 
116
120
  private func manageProgress(_ index: Int) {

5

なぜインジケーターが起動しないかどうかの推測を追記しました。

2023/01/07 02:44

投稿

uni2
uni2

スコア254

test CHANGED
@@ -1,3 +1,7 @@
1
+ manageProgress() メソッドが実行されていないため
2
+ インジケーターが起動していないと推測します。
3
+
4
+ また、
1
5
  NavigationStack の外に ProgressView() を出す方針で
2
6
  組んでみては如何でしょうか?
3
7
 

4

誤りを修正しました。

2023/01/07 02:34

投稿

uni2
uni2

スコア254

test CHANGED
@@ -87,10 +87,11 @@
87
87
  .onAppear {
88
88
  DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
89
89
  getThread()
90
+ ForEach(self.threads, id: \.objectId) { thread in
91
+ isNavigationLinkActive.append(false)
92
+ }
90
93
  }
91
- ForEach(self.threads, id: \.objectId) { thread in
94
+
92
- isNavigationLinkActive.append(false)
93
- }
94
95
  }
95
96
  .sheet(isPresented: $showModal) {
96
97
  AddThreadView()
@@ -98,6 +99,9 @@
98
99
  .onChange(of: showModal, perform: { _ in
99
100
  if !showModal {
100
101
  getThread()
102
+ ForEach(self.threads, id: \.objectId) { thread in
103
+ isNavigationLinkActive.append(false)
104
+ }
101
105
  }
102
106
  })
103
107
  .alert(isPresented: $showAlert, content: {

3

manageProgress メソッドが実行されていなかったため、実行されるよう修正しました。

2023/01/07 02:30

投稿

uni2
uni2

スコア254

test CHANGED
@@ -45,7 +45,6 @@
45
45
 
46
46
  動くか分かりませんが
47
47
  次のような内容です。
48
- (manageProgress() メソッドが呼ばれていないため、動かないような気はします)
49
48
 
50
49
  ```Swift
51
50
  import SwiftUI
@@ -56,58 +55,60 @@
56
55
  @State private var showModal = false
57
56
  @State private var showAlert = false
58
57
  @State var isPresentedProgressView = false
58
+ @State var isNavigationLinkActive: [Bool] = []
59
59
  var body: some View {
60
+ NavigationView {
61
+ List {
62
+ ForEach(0 ..< threads.count, id: \.self) { i in
60
- ZStack {
63
+ ZStack {
61
- NavigationView {
62
- List {
63
- ForEach(self.threads, id: \.objectId) { thread in
64
+ Button(action: manageProgress(i)){}
64
- NavigationLink(destination: ThreadView(thread: thread)) {
65
+ NavigationLink(destination: ThreadView(thread: threads[i]), isActive: $isNavigationLinkActive[i]) {
65
66
  ThreadListRow(thread: thread)
66
67
  }
67
68
  }
68
- .onDelete(perform: delete)
69
69
  }
70
+ .onDelete(perform: delete)
71
+ }
70
- .navigationBarTitle("掲示板", displayMode: .inline)
72
+ .navigationBarTitle("掲示板", displayMode: .inline)
71
- .toolbar {
73
+ .toolbar {
72
- ToolbarItem(placement: .navigationBarTrailing) {
74
+ ToolbarItem(placement: .navigationBarTrailing) {
73
- Button {
75
+ Button {
74
- showModal.toggle()
76
+ showModal.toggle()
75
- } label: {
77
+ } label: {
76
- Image(systemName: "plus")
78
+ Image(systemName: "plus")
77
- .resizable()
79
+ .resizable()
78
- .padding(6)
80
+ .padding(6)
79
- .frame(width: 24, height: 24)
81
+ .frame(width: 24, height: 24)
80
- .foregroundColor(.blue)
82
+ .foregroundColor(.blue)
81
- }
82
83
  }
83
84
  }
84
85
  }
86
+ }
85
- .onAppear {
87
+ .onAppear {
86
- DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
88
+ DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
87
- getThread()
89
+ getThread()
88
- }
89
90
  }
91
+ ForEach(self.threads, id: \.objectId) { thread in
90
- .sheet(isPresented: $showModal) {
92
+ isNavigationLinkActive.append(false)
91
- AddThreadView()
92
93
  }
93
- .onChange(of: showModal, perform: { _ in
94
- if !showModal {
95
- getThread()
96
- }
97
- })
98
- .alert(isPresented: $showAlert, content: {
99
- Alert(title: Text("削除に失敗しました。権限がないようです。"))
100
- })
101
- if isPresentedProgressView {
102
- Color.gray.opacity(0.5)
103
- ProgressView("読み込み中")
104
- } //追記
105
94
  }
95
+ .sheet(isPresented: $showModal) {
96
+ AddThreadView()
97
+ }
98
+ .onChange(of: showModal, perform: { _ in
99
+ if !showModal {
100
+ getThread()
101
+ }
102
+ })
103
+ .alert(isPresented: $showAlert, content: {
104
+ Alert(title: Text("削除に失敗しました。権限がないようです。"))
105
+ })
106
106
  }
107
107
 
108
- private func manageProgress() {
108
+ private func manageProgress(_ index: Int) {
109
109
  // ProgressView 表示
110
110
  isPresentedProgressView = true
111
+ isNavigationLinkActive[index] = true
111
112
  // 3秒後に非表示に
112
113
  DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
113
114
  self.isPresentedProgressView = false

2

補足を追記しました。

2023/01/07 02:18

投稿

uni2
uni2

スコア254

test CHANGED
@@ -45,6 +45,7 @@
45
45
 
46
46
  動くか分かりませんが
47
47
  次のような内容です。
48
+ (manageProgress() メソッドが呼ばれていないため、動かないような気はします)
48
49
 
49
50
  ```Swift
50
51
  import SwiftUI

1

変更例を挙げました。

2023/01/07 02:11

投稿

uni2
uni2

スコア254

test CHANGED
@@ -42,3 +42,96 @@
42
42
  }
43
43
  }
44
44
  ```
45
+
46
+ 動くか分かりませんが
47
+ 次のような内容です。
48
+
49
+ ```Swift
50
+ import SwiftUI
51
+ import NCMB
52
+
53
+ struct ThreadListView: View {
54
+ @State private var threads: [NCMBObject] = []
55
+ @State private var showModal = false
56
+ @State private var showAlert = false
57
+ @State var isPresentedProgressView = false
58
+ var body: some View {
59
+ ZStack {
60
+ NavigationView {
61
+ List {
62
+ ForEach(self.threads, id: \.objectId) { thread in
63
+ NavigationLink(destination: ThreadView(thread: thread)) {
64
+ ThreadListRow(thread: thread)
65
+ }
66
+ }
67
+ .onDelete(perform: delete)
68
+ }
69
+ .navigationBarTitle("掲示板", displayMode: .inline)
70
+ .toolbar {
71
+ ToolbarItem(placement: .navigationBarTrailing) {
72
+ Button {
73
+ showModal.toggle()
74
+ } label: {
75
+ Image(systemName: "plus")
76
+ .resizable()
77
+ .padding(6)
78
+ .frame(width: 24, height: 24)
79
+ .foregroundColor(.blue)
80
+ }
81
+ }
82
+ }
83
+ }
84
+ .onAppear {
85
+ DispatchQueue.main.asyncAfter(deadline: .now()+0.3) {
86
+ getThread()
87
+ }
88
+ }
89
+ .sheet(isPresented: $showModal) {
90
+ AddThreadView()
91
+ }
92
+ .onChange(of: showModal, perform: { _ in
93
+ if !showModal {
94
+ getThread()
95
+ }
96
+ })
97
+ .alert(isPresented: $showAlert, content: {
98
+ Alert(title: Text("削除に失敗しました。権限がないようです。"))
99
+ })
100
+ if isPresentedProgressView {
101
+ Color.gray.opacity(0.5)
102
+ ProgressView("読み込み中")
103
+ } //追記
104
+ }
105
+ }
106
+
107
+ private func manageProgress() {
108
+ // ProgressView 表示
109
+ isPresentedProgressView = true
110
+ // 3秒後に非表示に
111
+ DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
112
+ self.isPresentedProgressView = false
113
+ }
114
+ }
115
+
116
+ private func getThread() {
117
+ let query = NCMBQuery.getQuery(className: "Thread")
118
+ let results = query.find()
119
+ switch results {
120
+ case let .success(ary):
121
+ threads = ary
122
+ case .failure(_): break
123
+ }
124
+ }
125
+
126
+ private func delete(at offsets: IndexSet) {
127
+ let thread = self.threads[Array(offsets)[0]] as NCMBObject
128
+ let results = thread.delete()
129
+ switch results {
130
+ case .success(_):
131
+ getThread()
132
+ case .failure(_):
133
+ showAlert = true
134
+ }
135
+ }
136
+ }
137
+ ```