簡體   English   中英

TableViewController中的警報視圖文本字段通知

[英]alert view textfield notification in TableViewController

我有帶條形按鈕項的TableViewController。 這是條形按鈕項目的代碼。

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

我正在使用警報控制器將新的String添加到tableview中。 並僅在文本字段不為空時使用通知來啟用“保存”按鈕。 此確切的代碼適用於其中具有tableview的view controller。 但是當我用TableViewController測試時。 它不起作用。 我收到以下錯誤。

 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 

如果我從添加觀察者的內部刪除textfield.text,則一切正常。 但是我只想在文本字段不為空的情況下啟用保存按鈕。 和應用程序不允許我這樣做。

如果您需要更多信息,那么我很樂意在GitHub上共享文件。 讓我知道。

更換

saveAction.enabled = textField.text != ""

在您的通知閉包中,其中包含以下內容:

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

希望有幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM