【问题标题】:UIAlertController in swiftswift中的UIAlertController
【发布时间】:2015-08-03 04:49:52
【问题描述】:

我正在快速创建一个带有几个文本字段的view controller 和一个确认用户输入的accept buttonaccept button 还会检查是否有任何文本字段为空。如果是这样,它会弹出一个alert,说类似it cannot be empty。如果不为空,它将存储输入,然后跳转到另一个视图。

我创建了一个名为 checEmpty() 的独立函数,如下所示:

    func checEmpty(title: String, object: UITextField) -> (Bool) {
        if object.text.isEmpty {
            let alertController = UIAlertController(title: "Invalid input", 
                message:"\(title) cannot be empty", 
                preferredStyle: UIAlertControllerStyle.Alert)

            alertController.addAction(UIAlertAction(title: "Dismiss", 
                style: UIAlertActionStyle.Default)

            self.presentViewController(alertController, animated: true, completion: nil)

            return false
        } else {
            return true
        }
    }

我在acceptButton 操作中调用了这个函数:

   @IBAction func acceptButton(sender: UIButton){
         if(checEmpty("Event", object: eventName) && checEmpty("Priority", object: Priority)
         { 
                //if not empty, confirm the user input
               // ...
         }

当我运行它时,警报消息可以正常工作,但由于某种原因控制台显示如下:

2015-08-03 12:11:50.656 FinishItToday[13777:688070] > 的窗口不等于 的视图窗口!

谁能告诉我为什么会出现这个警告?非常感谢!

PS。 我想要它做的是,如果任何文本字段为空,则显示警报,然后留在同一页面。如果它们都不是空的,则执行 segue 并切换到另一个视图。上面的代码工作正常,除了警告。

【问题讨论】:

  • 好吧,我仍然不知道该警告的含义,但我通过重写 shouldPerformSegue 函数以某种方式解决了问题,让 segue 在有空文本字段时停止工作。现在我相信 performViewController() 和 segue 之间可能存在一些冲突。

标签: ios swift uialertcontroller


【解决方案1】:

这是您的工作代码:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var eventName: UITextField!
    @IBOutlet weak var Priority: UITextField!

    @IBAction func acceptButton(sender: UIButton){

        if checEmpty("Event", object: eventName) && checEmpty("Priority", object: Priority){
            println("Both Text Fields Are Empty")
        }
    }

    func checEmpty(title: String, object: UITextField) -> (Bool) {
        if object.text.isEmpty {

            var Alert = UIAlertController(title: "Invalid input", message: "\(title) cannot be empty", preferredStyle: UIAlertControllerStyle.Alert)

            Alert.addAction(UIAlertAction(title: "Dismiss", style: .Cancel, handler: { action in
                println("Click of cancel button")
            }))
            self.presentViewController(Alert, animated: true, completion: nil)

            return false
        } else {
            return true
        }
    }
}

【讨论】:

  • 您应该考虑在您的答案中添加一些注释,解释问题所在以及您的代码如何解决此问题
  • 很抱歉,警告仍然存在。你能告诉我问题出在哪里吗?
  • 感谢您的回答!它以某种方式启发了我,因为您的代码和我的代码之间的唯一区别是我的代码也有 segue。所以我想self.presentViewController() 和segue 之间可能存在一些冲突。现在我解决了这个问题!谢谢!
【解决方案2】:

使用此代码在 swift 中提醒视图控制器。它可能会帮助你。

 import UIKit

 protocol alertViewDelegate {
     func actionActive(index:Int, tag:Int)
 }

 class AlertView: NSObject {

 var delegate:alertViewDelegate!

 func showAlert(title:String, message:String, actionName:NSArray, tag:Int) ->    UIAlertController {
         var alertController:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)

         for str: AnyObject in actionName {
         let alertAction:UIAlertAction = UIAlertAction(title: str as! String, style: UIAlertActionStyle.Default, handler: { (action) -> Void in
                 self.delegate.actionActive(actionName.indexOfObject(str), tag:tag)
             })
             alertController.addAction(alertAction)
         }
         return alertController;
     }

 }

【讨论】:

  • 你能告诉我为什么会有这样的警告吗?
  • 通过这个。你会得到答案。 stackoverflow.com/questions/25677447/…
  • 您正在使用 self.presentViewController(alertController, animated: true, completion: nil) 来呈现新控制器,并且还使用 return false 给您的视图控制器。这样对吗?请更改您的方法,它将解决。
  • 谢谢。我确实忘记了该按钮还附加了一个segue。该链接很有用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多