Last active
June 26, 2019 15:58
-
-
Save yanil3500/963113c3db050ca0894666137d0931d1 to your computer and use it in GitHub Desktop.
[Getting Input From User] Code for obtaining input from the user in an iOS application
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
// NEEDS to be used as an Xcode Snippet. | |
func getInputFromUser(){ | |
let alert = UIAlertController(title: <#Domain-specific title: String>, message: "", preferredStyle: .alert) | |
var textField = UITextField() | |
// Using the above textField reference, we can extend the scope of the alertTextField in the closure so that it can be accessed outside of its | |
// narrow scope. | |
alert.addTextField { alertTextField in | |
alertTextField.placeholder = <#Domain-specific placeholder: String> | |
textField = alertTextField | |
} | |
// Defining actions of OK and Cancel Buttons | |
let okButtonAction = UIAlertAction(title: "OK", style: .default) { _ in | |
// what will happen once the user clicks the Add New Todo button on our UIAlert | |
// grab the text from the text field and assign to local constant if the value is not an empty string. | |
guard let textFieldText = textField.text, !textFieldText.isEmpty else { return } | |
// Do stuff with the input | |
} | |
let cancelButtonAction = UIAlertAction(title: "Cancel", style: .cancel) { _ in | |
alert.dismiss(animated: true, completion: nil) | |
} | |
// Adding actions to alertVC | |
alert.addAction(okButtonAction) | |
alert.addAction(cancelButtonAction) | |
// Present Alert | |
present(alert, animated: true, completion: nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment