[swift] 모달 뷰에 사용자 입력을 받는 방법은?
아래는 UIAlertController를 사용하여 모달 뷰에 텍스트 필드를 추가하는 간단한 예제 코드입니다.
let alertController = UIAlertController(title: "제목", message: "메시지", preferredStyle: .alert)
// 텍스트 필드 추가
alertController.addTextField { (textField) in
textField.placeholder = "사용자 입력"
}
let okAction = UIAlertAction(title: "확인", style: .default) { (action) in
if let userInput = alertController.textFields?.first?.text {
// 사용자 입력 사용
print("사용자 입력: \(userInput)")
}
}
alertController.addAction(okAction)
// 모달 뷰 표시
present(alertController, animated: true, completion: nil)
위 예제에서 UIAlertController를 사용하여 모달 뷰를 만들고, addTextField 메서드를 사용하여 텍스트 필드를 추가했습니다. 그리고 확인 버튼을 누를 때 입력된 값을 가져와 활용하고 있습니다.
참고 문헌:
- https://developer.apple.com/documentation/uikit/uialertcontroller