我有带按钮项的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)
}
我正在使用警报控制器向tableview添加新的String。并且仅当textfield不为空时才使用通知启用“保存”按钮。这个确切的代码适用于视图控制器,里面有tableview。但是当我用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,那么一切正常。但是我想只在textfield不为空时启用保存按钮。应用程序允许我这样做。
如果您需要更多信息,我很乐意在GitHub上分享文件。 让我知道。
答案 0 :(得分:0)
替换
saveAction.enabled = textField.text != ""
通知闭包内的,内容如下:
let textField = notification.object as! UITextField
saveAction.enabled = !textField.text.isEmpty
希望有所帮助!