swift alertview textfield - how to retrieve the text entered

3.5k views Asked by At

I'm showing an alert view to be able to rename a Book when selected. I am Having issues retrieving the input from the user.

here is my code:

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)


}

the spot I'm running into trouble is:

        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")
        })

i hope this is a simple fix, and i appreciate any help. thanks!

1

There are 1 answers

0
Nate Cook On

You need to add a completion handler for that alert controller that gets the new string and uses it to update your book title. Something like:

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")
    }
})