回答編集履歴

2

コード改善

2022/10/30 09:12

投稿

TakeOne
TakeOne

スコア6299

test CHANGED
@@ -3,8 +3,6 @@
3
3
  自分自身の勉強にもなるので、ちょっと作ってみました。
4
4
 
5
5
  ``` swift
6
- import SwiftUI
7
-
8
6
  struct AlertController: UIViewControllerRepresentable {
9
7
  @Binding var isPresented: Bool
10
8
  @Binding var text: String
@@ -46,24 +44,21 @@
46
44
  }
47
45
 
48
46
  // キャンセルボタンアクション
49
- let cancelAction = UIAlertAction(title: "キャンセル", style: .cancel) { _ in
47
+ let cancelAction = UIAlertAction(title: "キャンセル", style: .cancel) {_ in }
50
- isPresented = false
51
- context.coordinator.alert = nil
52
- }
53
48
 
54
49
  // OKボタンアクション
55
50
  let okAction = UIAlertAction(title: "OK", style: .default) { _ in
56
51
  let textField = alert.textFields!.first!
57
52
  self.text = textField.text!
58
- isPresented = false
59
- context.coordinator.alert = nil
60
53
  }
61
54
  okAction.isEnabled = false
62
55
 
63
56
  alert.addAction(cancelAction)
64
57
  alert.addAction(okAction)
65
58
 
66
- uiViewController.present(alert, animated: true)
59
+ uiViewController.present(alert, animated: true) {
60
+ isPresented = false
61
+ }
67
62
  }
68
63
  }
69
64
  }

1

説明修正

2022/10/30 02:45

投稿

TakeOne
TakeOne

スコア6299

test CHANGED
@@ -1,4 +1,4 @@
1
- これは、SwiftUIのAlert機能では(今のところ)無理だろうと思います。どうしてもやりたいなら、UIKitのUIAlertControllerをSwiftUIから使用し、その処理を制御(UIActionのisEnabledをON/OFF)する必要があると思います。
1
+ これは、SwiftUIのAlert機能では(今のところ)無理だろうと思います。どうしてもやりたいなら、UIKitのUIAlertControllerをSwiftUIから使用し、その処理を制御(UIAlertActionのisEnabledをON/OFF)する必要があると思います。
2
2
 
3
3
  自分自身の勉強にもなるので、ちょっと作ってみました。
4
4