複数のUIAlertActionsを持つUIAlertControllerがあります。選択したアクションのUIAlertActiontitleプロパティにアクセスしたいと思います。
let ac = UIAlertController(title: "Choose Filter", message: nil, preferredStyle: .actionSheet)
ac.addAction(UIAlertAction(title: "CIBumpDistortion", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIGaussianBlur", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIPixellate", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CISepiaTone", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CITwirlDistortion", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIUnsharpMask", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "CIVignette", style: .default, handler: setFilter))
ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
ac.titleプロパティにアクセスするには、AlertControllertitleプロパティにのみアクセスしてください。
Appleのドキュメントを見ると、次のようなものを使用する必要があるようです。
var title: String? { get }
ただし、この構文の使用方法についてはよくわかりません。
ドキュメントの構文は、title
プロパティがどのように宣言されているかを示しており、コードでプロパティを参照する方法は示していません(ただし、いくつかの洞察を得ることができます)。
これはプロパティである.title
ため、のインスタンスがある場合はドット表記でアクセスできますUIAlertAction
。幸いなことUIAlertAction
に、handler
関数に渡されたパラメータの選択されたインスタンスにアクセスできます。
アラートアクションのハンドラーで、つまりsetFilter
、そのパラメーターのにアクセスできます.title
。これが選択されたアクションのタイトルになります。
func setFilter(_ action: UIAlertAction) {
let selectedActionTitle = action.title
...
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加