swift alertview textfield - 如何检索输入的文本
Posted
技术标签:
【中文标题】swift alertview textfield - 如何检索输入的文本【英文标题】:swift alertview textfield - how to retrieve the text entered 【发布时间】:2014-11-14 20:32:46 【问题描述】:我正在显示一个警报视图,以便在选择时能够重命名一本书。我在检索用户输入时遇到问题。
这是我的代码:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
let optionMenu = UIAlertController(title: nil, message: "Choose Option", preferredStyle: .ActionSheet)
let newBookAction = UIAlertAction(title: "Add New Book", style: .Default, handler:
(alert: UIAlertAction!) -> Void in
var alert = UIAlertController(title: "Add New Book", message: "What is the Name of the New Book?", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addTextFieldWithConfigurationHandler (textField) in
textField.placeholder = "New Book Name"
self.currentShelf.addNewBook(Book(bookName: self.alert.textFields[0] as String))
self.presentViewController(alert, animated: true, completion: nil)
self.tableView.reloadData()
println("Book Added")
)
let deleteAction = UIAlertAction(title: "Delete", style: .Default, handler:
(alert: UIAlertAction!) -> Void in
self.currentShelf.allBooksOnShelf.removeAtIndex(indexPath.row)
self.tableView.reloadData()
println("Book Deleted")
)
let updateAction = UIAlertAction(title: "Update", style: .Default, handler:
(alert: UIAlertAction!) -> Void in
println("Book Updated")
)
let readAction = UIAlertAction(title: "Read", style: .Default, handler:
(alert: UIAlertAction!) -> Void in
if self.currentShelf.allBooksOnShelf[indexPath.row].hasBeenRead == false
self.currentShelf.allBooksOnShelf[indexPath.row].hasBeenRead = true
var alert = UIAlertController(title: "Read Book", message: "Thanks For Reading!", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
println("Book Read")
else
self.currentShelf.allBooksOnShelf[indexPath.row].hasBeenRead = false
var alert = UIAlertController(title: "Unread Book", message: "You Just UnRead A Book...", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
println("Book Unread")
)
let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler:
(alert: UIAlertAction!) -> Void in
println("Cancelled")
)
optionMenu.addAction(newBookAction)
optionMenu.addAction(readAction)
optionMenu.addAction(updateAction)
optionMenu.addAction(deleteAction)
optionMenu.addAction(cancelAction)
self.presentViewController(optionMenu, animated: true, completion: nil)
我遇到麻烦的地方是:
let newBookAction = UIAlertAction(title: "Add New Book", style: .Default, handler:
(alert: UIAlertAction!) -> Void in
var alert = UIAlertController(title: "Add New Book", message: "What is the Name of the New Book?", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addTextFieldWithConfigurationHandler (textField) in
textField.placeholder = "New Book Name"
self.currentShelf.addNewBook(Book(bookName: self.alert.textFields[0] as String))
self.presentViewController(alert, animated: true, completion: nil)
self.tableView.reloadData()
println("Book Added")
)
我希望这是一个简单的修复,我感谢任何帮助。谢谢!
【问题讨论】:
【参考方案1】:您需要为获取新字符串并使用它来更新您的书名的警报控制器添加一个完成处理程序。比如:
let newBookAction = UIAlertAction(title: "Add New Book", style: .Default, handler:
(alert: UIAlertAction!) -> Void in
var alert = UIAlertController(title: "Add New Book", message: "What is the Name of the New Book?", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
alert.addTextFieldWithConfigurationHandler (textField) in
textField.placeholder = "New Book Name"
self.presentViewController(alert, animated: true)
let textField = alert.textFields[0] as UITextField
self.currentShelf.addNewBook(Book(bookName: textField.text))
self.tableView.reloadData()
println("Book Added")
)
【讨论】:
以上是关于swift alertview textfield - 如何检索输入的文本的主要内容,如果未能解决你的问题,请参考以下文章
如何将 TextField 和 CheckBox 添加到同一个 AlertView
在 iOS8 Swift 中对齐左 AlertView 消息
Xcode Swift alertView 在进入根控制器后消失