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

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

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

Q&A

解決済

3回答

2324閲覧

SwiftUIでのアラートのエラー

apple_shinjya_

総合スコア8

0グッド

0クリップ

投稿2019/09/11 07:53

SwiftUI

1import SwiftUI 2 3struct ContentView: View { 4 var body: some View { 5 HStack { 6 HStack { 7 Button(action: { 8 .presentation(ActionSheet(title: Text("Placeholder")) 9 )}) { 10 Text("Button") 11 .font(.largeTitle) 12 } 13 } 14 } 15 } 16} 17 18struct ContentView_Previews: PreviewProvider { 19 static var previews: some View { 20 ContentView() 21 } 22}

SwiftUIでこのようなコードを書いたところ、

SwiftUI

1.presentation(ActionSheet(title: Text("Placeholder"))

この部分に

Value of tuple type 'Void' has no member 'presentation'

このようなエラーが出るのですが意味と解決方法を教えてください(初心者です)

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

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

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

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

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

guest

回答3

0

ベストアンサー

このコードを実行したところボタンを押すとアプリが落ちるのですが...

iPad だけで問題が起こるなら、不具合の可能性があります。
検索した所、8月末の情報ですが StackOverflow(英語版)にそれっぽい記載があります。

Present ActionSheet in SwiftUI on iPad
It is a known bug of beta. Just wait for a fix.


エラーなどは出てきません

試しに iPad の Simulator で実行した所、以下のエラーメッセージが出力されます。

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Your application has presented a UIAlertController (<UIAlertController: 0x7ff77602a200>) of style UIAlertControllerStyleActionSheet from _TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentV6TestUI11ContentViewGVS_30_EnvironmentKeyWritingModifierCSo22NSManagedObjectContext___ (<_TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentV6TestUI11ContentViewGVS_30_EnvironmentKeyWritingModifierCSo22NSManagedObjectContext___: 0x7ff775507d00>). The modalPresentationStyle of a UIAlertController with this style is UIModalPresentationPopover. You must provide location information for this popover through the alert controller's popoverPresentationController. You must provide either a sourceView and sourceRect or a barButtonItem. If this information is not known when you present the alert controller, you may provide it in the UIPopoverPresentationControllerDelegate method -prepareForPopoverPresentation.' *** First throw call stack: 〜以下省略〜

このメッセージで検索した所、コントローラーに sourceView を指定するといった対処方法が見つかりますが、SwiftUI だとこの指定が行えません。

iPadOS や XCode のアップデートをのんびり待つのが吉だと思います。

投稿2019/09/24 01:40

kechizenya

総合スコア34

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

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

0

Value of tuple type 'Void' has no member 'presentation'
このようなエラーが出るのですが意味と解決方法を教えてください(初心者です)

バージョンは忘れましたが、ActionSheet や Alert、Modal (Sheet) の表示方法は途中で変更されました。

現在は .actionSheet や .alert などで記述します。

Swift

1struct ContentView: View { 2 @State var show = false // 表示のオン・オフを切り替えるフラグ 3 4 var body: some View { 5 HStack { 6 Button(action: { 7 self.show.toggle() // フラグをオンに変更 8 }) { 9 Text("Button") 10 .font(.largeTitle) 11 } 12 } 13 .actionSheet(isPresented: self.$show) { 14 // フラグがオンの場合、ここに定義した content が表示される 15 ActionSheet(title: Text("Placeholder")) 16 } 17 } 18}

投稿2019/09/20 08:48

kechizenya

総合スコア34

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

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

apple_shinjya_

2019/09/22 01:38

このコードを実行したところボタンを押すとアプリが落ちるのですが... ちなみにiPad Pro 2018(iOS13.1 beta4)/MacBook Pro 2017(macOS Catalina beta 8)です。
guest

0

初心者であれば、こちらを参考にしては?

https://www.ioscreator.com/tutorials/swiftui-action-sheet-tutorial

投稿2019/09/11 08:58

t_obara

総合スコア5488

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

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

apple_shinjya_

2019/09/11 12:34 編集

ありがとうございます。英語は苦手ですが頑張ります
3265462164

2019/09/18 02:08

いえ、t_obaraというかたはあまり知識無いですよ。 いつも適当なURL貼るだけの低能ですから。 私の所にも沸きました。 きちんと教えてくれる有識者の方も多数いるので、少しお待ちになられては?
apple_shinjya_

2019/09/19 03:35

わかりました...そうしてみます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問