【Swift】 TextField가 있는 Alert를 표시시키는 방법
구현 이미지
이러한 타이틀, 부제목, 입력란, 취소, 추가로 구성된 Alert를 작성해 갑니다.

Storyboard
버튼을 누르면 Alert가 표시되도록 하려면 원하는 위치에 버튼을 하나 추가하세요.

소스 코드
ViewController.Swift
import UIKit
//UITextFieldDelegateを追加する
class ViewController: UIViewController,UITextFieldDelegate {
//StoryboardのbuttonをAction接続する
@IBAction func aleat(_ sender: Any) {
let alert = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
alert.title = "ここにタイトル"
alert.message = "メッセージ"
alert.addTextField(configurationHandler: {(textField) -> Void in
textField.delegate = self
})
//追加ボタン
alert.addAction(
UIAlertAction(
title: "追加",
style: .default,
handler: {(action) -> Void in
self.hello(action.title!)
})
)
//キャンセルボタン
alert.addAction(
UIAlertAction(
title: "キャンセル",
style: .cancel,
handler: {(action) -> Void in
self.hello(action.title!)
})
)
//アラートが表示されるごとにprint
self.present(
alert,
animated: true,
completion: {
print("アラートが表示された")
})
}
func hello(_ msg:String){
print(msg)
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
빌드하고 확인해 봅시다.
구현 이미지와 같은 Alert를 표시 할 수 있었습니까?
title 부분은 좋아하는 문장으로 바꾸어 원하는 Alert를 표시시킬 수 있습니다.
할 수 있었어! 그렇다면 LGTM을 누르면 내가 정말 기뻐합니다!
Reference
이 문제에 관하여(【Swift】 TextField가 있는 Alert를 표시시키는 방법), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/oskmr/items/6de113c586286a76d2d3텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)