質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

485閲覧

NavigationLinkを用いた別Viewへの遷移

pon3428

総合スコア14

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2022/12/12 02:55

編集2022/12/12 02:58

前提

ListはNavigationLinkの中で使用しています。

var body: some View {
NavigationView {
List{Section(header: Text("テキスト")){ForEach(0..<self.pinned.count) {index in NavigationLink(destination: AnotherView, label: {Text(pinned[index])}}}}
}
}}

実現したいこと

List内のNavigationLinkでの別Viewへの遷移

発生している問題・エラーメッセージ

Type 'AnotherView.Type' cannot conform to 'View' Cannot convert value of type 'Binding<[String]>' to expected argument type 'Binding<String>'

該当のソースコード

SwiftUI

1 2struct ContentView: View { 3 4@State var pinned = [""] 5 6var body: some View { 7NavigationView { 8List{Section(header: Text("テキスト")){ForEach(0..<self.pinned.count) {index in NavigationLink(destination: AnotherView, label: {Text(pinned[index])}}}} 9} 10}} 11 12--- 13 14struct AnotherView: View { 15 16@State var textEditorText: [String] 17 18var body: some View { 19 20TextEditor(text: $textEditorText).frame(width: 200, height: 200) 21 22}.onAppear{textEditorText = [index]} 23 24} 25

試したこと

補足情報(FW/ツールのバージョンなど)

Xcodeはバージョン14.1です。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

このエラーは
TextEditor に String の配列を入れているため
発生しているものと思われます。

@State var textEditorText: String
のように String で宣言することで
このエラーは消えると思われます。

エラーを全て潰したサンプルコードを添付します。

SwiftUI

1import SwiftUI 2 3var passedPinned: [String]? 4 5struct ContentView: View { 6 7 @State var pinned = ["項目1", "項目2", "項目3"] 8 9 var body: some View { 10 NavigationView { 11 List{ 12 Section(header: Text("テキスト")) 13 { 14 ForEach(0..<self.pinned.count) 15 { index in 16 NavigationLink(destination: AnotherView(textEditorText: pinned[index], index: index), label: {Text(pinned[index])}) 17 .onAppear(perform: { 18 if let recive = passedPinned { 19 pinned = recive 20 } 21 }) 22 .onDisappear(perform: {passedPinned = pinned}) 23 } 24 } 25 } 26 } 27 } 28} 29 30struct AnotherView: View { 31 32 @State var textEditorText: String 33 var index = 0 34 35 var body: some View { 36 TextEditor(text: $textEditorText) 37 .frame(width: 200, height: 200) 38 .onChange(of: textEditorText){ value in 39 passedPinned![index] = value 40 } 41 } 42} 43 44struct ContentView_Previews: PreviewProvider { 45 static var previews: some View { 46 ContentView() 47 } 48}

投稿2022/12/12 04:32

uni2

総合スコア254

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問