我在用斯威夫特2。这个问题与iOS9有关。简而言之:-如果单击了“文件重命名”按钮,但文件名仍然无效,则是否需要再次显示警报,或者是否有更智能的方法来处理此问题?全部:-保存一个从iCloud导入的文件,如果在.cc>中已经存在一个同名文件(UIAlertController),我将提交一个名为alertController的.lastPathComponent。/Documents有两个动作名为UIAlertController和Cancel和aRename。如果文件名已经存在,则提示用户取消或重命名。此问题与验证新名称以及在文件名有效之前重新呈现(或不解除)UIAlertController有关:如果用户单击重命名按钮,文件名仍然相同,或者与已经存在的另一个文件相同,那么我希望再次显示UIArrtTube。或者更好的是,在文件名有效之前,它不会被删除。我做这件事的方法(我唯一知道的方法)是添加一个名为.addTextFieldWithConfigurationHandler的func来显示UIAlertController。当文件名已经存在(或未被更改)时,单击重命名处理程序时,将调用该“”。(与从操作中再次显示UIAlertController相同)。我的问题是:-我的代码做了我想做的,但是有谁能建议一个更整洁、更不笨拙的方法来实现这个结果-而不必再次呈现presentAlertController?这是相关代码。请注意,整个部分都在另一个函数的完成处理程序内,因此需要对func进行各种引用,以及为什么UIAlertController代码在self内(必须从主队列调用)。//...if checkFileExists(saveURL) { // saveURL: NSURL dispatch_async(dispatch_get_main_queue()) { let message = "File named `\(saveURL.lastPathComponent!)` already exists. Please import using a new name or else cancel." let alertController = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler { textField -> Void in textField.text = saveURL.lastPathComponent! // it presents a text field containing the file name which needs to be changed } func presentAlertController() { self.presentViewController(alertController, animated: true) {} } alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) alertController.addAction(UIAlertAction(title: "Rename", style: .Default) { action -> Void in saveURL = saveURL.URLByDeletingLastPathComponent!.URLByAppendingPathComponent((alertController.textFields?.first!.text)!) if checkFileExists(saveURL) { presentAlertController() // currently it is calling a function to present the UIAlertController again if the file still exists when the button is clicked } else { saveXML(saveURL, dataObject: self.myThing) self.fileTableView.reloadData() } }) presentAlertController() // this will be the first time that this called }}//... (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 因此,您可以将目标添加到编辑文本字段时将调用的文本字段,并且在该函数中,您可以检查用户是否键入了有效的名称。问题是你需要访问alertController才能禁用“重命名”按钮。您可以通过在视图控制器的顶部创建一个属性来实现这一点,如下所示:var alertController: UIAlertController!然后修改你发布的代码如下://...if checkFileExists(saveURL) { // saveURL: NSURL dispatch_async(dispatch_get_main_queue()) { let message = "File named `\(saveURL.lastPathComponent!)` already exists. Please import using a new name or else cancel." //just assigning here, not redeclaring (get rid of "let") alertController = UIAlertController(title: "", message: message, preferredStyle: UIAlertControllerStyle.Alert) alertController.addTextFieldWithConfigurationHandler { textField -> Void in textField.text = saveURL.lastPathComponent! // it presents a text field containing the file name which needs to be changed //Add the target here, calls on checkString textField.addTarget(self, action: "checkString:", forControlEvents: UIControlEvents.EditingChanged) } func presentAlertController() { self.presentViewController(alertController, animated: true) {} } alertController.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)) alertController.addAction(UIAlertAction(title: "Rename", style: .Default) { action -> Void in saveURL = saveURL.URLByDeletingLastPathComponent!.URLByAppendingPathComponent((alertController.textFields?.first!.text)!) if checkFileExists(saveURL) { presentAlertController() // currently it is calling a function to present the UIAlertController again if the file still exists when the button is clicked } else { saveXML(saveURL, dataObject: self.myThing) self.fileTableView.reloadData() } }) //Disable the "Rename" button to start, remove this line if you don't want that to happen (alertController.actions as! [UIAlertAction])[1].enabled = false presentAlertController() // this will be the first time that this called }}//...然后,您必须创建一个checkString函数(显然,只要您还更改了将目标添加到文本字段的行上的选择器,您就可以随意重命名它)。这里有一些代码可以给你一个想法,但是你必须在这里写你自己的东西。func checkString(sender: UITextField) { //Pretty safe assumption you don't want an empty string as a name if sender.text == "" { (alertController.actions as! [UIAlertAction])[1].enabled = false } //As soon as the user types something valid, the "Rename" button gets enabled else { (alertController.actions as! [UIAlertAction])[1].enabled = true }}这段代码已经过测试,但不是很严格,因此如果您有问题或它有效,请进行注释。 (adsbygoogle = window.adsbygoogle || []).push({});
10-08 03:20