简体   繁体   中英

alert view textfield notification in TableViewController

I have TableViewController with bar button item. This is the code for bar button item.

  @IBAction func addPressed(sender: UIBarButtonItem) { var alertController = UIAlertController(title: "New Student", message: "Name", preferredStyle: UIAlertControllerStyle.Alert) var saveAction = UIAlertAction(title: "Save", style: UIAlertActionStyle.Default) { (action: UIAlertAction!) -> Void in var textField = alertController.textFields![0] as! UITextField self.students.append(textField.text) self.tableView.reloadData() } saveAction.enabled = false var cancelAction = UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil) alertController.addTextFieldWithConfigurationHandler { (textField: UITextField!) -> Void in NSNotificationCenter.defaultCenter().addObserverForName(UITextFieldTextDidChangeNotification, object: textField, queue: NSOperationQueue.mainQueue(), usingBlock: { (notification: NSNotification!) -> Void in saveAction.enabled = textField.text != "" }) } alertController.addAction(saveAction) alertController.addAction(cancelAction) presentViewController(alertController, animated: true, completion: nil) } 

I am adding new String to tableview with alert controller. And using notification to enable Save button only if textfield is not empty. This exact code works with view controller with tableview inside it. But when I tested with TableViewController. Its not working. I am getting following error.

 CompileSwiftSources normal x86_64 com.apple.xcode.tools.swift.compiler cd "/Users/apoorv/Desktop/tableview testing" export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin" /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -target x86_64-apple-ios8.3 -incremental -module-name tableview_testing -Onone -sdk /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator8.3.sdk -g -module-cache-path /Users/apoorv/Library/Developer/Xcode/DerivedData/ModuleCache -I /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Products/Debug-iphonesimulator -F /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Products/Debug-iphonesimulator -c -j1 /Users/apoorv/Desktop/tableview\\ testing/tableview\\ testing/AppDelegate.swift /Users/apoorv/Desktop/tableview\\ testing/tableview\\ testing/TableViewController.swift -output-file-map /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/Objects-normal/x86_64/tableview\\ testing-OutputFileMap.json -parseable-output -serialize-diagnostics -emit-dependencies -emit-module -emit-module-path /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/Objects-normal/x86_64/tableview_testing.swiftmodule -Xcc -I/Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/swift-overrides.hmap -Xcc -iquote -Xcc /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/tableview\\ testing-generated-files.hmap -Xcc -I/Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/tableview\\ testing-own-target-headers.hmap -Xcc -I/Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/tableview\\ testing-all-target-headers.hmap -Xcc -iquote -Xcc /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/tableview\\ testing-project-headers.hmap -Xcc -I/Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Products/Debug-iphonesimulator/include -Xcc -I/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include -Xcc -I/Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/DerivedSources/x86_64 -Xcc -I/Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/DerivedSources -Xcc -DDEBUG=1 -emit-objc-header -emit-objc-header-path /Users/apoorv/Library/Developer/Xcode/DerivedData/tableview_testing-bcmobbjqfqwmmjhjbcdsnuctzpsi/Build/Intermediates/tableview\\ testing.build/Debug-iphonesimulator/tableview\\ testing.build/Objects-normal/x86_64/tableview_testing-Swift.h -Xcc -working-directory/Users/apoorv/Desktop/tableview\\ testing Command /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc failed with exit code 1 

If I remove textfield.text from inside add observers then everything works fine. But I want to enable save button only if textfield is not empty. And app doest allow me to do that.

If you need more information then I am happy to share file on GitHub. Let me know.

replace

saveAction.enabled = textField.text != ""

inside your notification closure with the following:

let textField = notification.object as! UITextField
saveAction.enabled = !textField.text.isEmpty

hope that helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM