簡體   English   中英

迅速的UIAlertController

[英]UIAlertController in swift

我正在快速創建一個view controller ,其中包含一些文本字段和一個accept button ,用於確認用戶的輸入。 accept 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並切換到另一個視圖。 上面的代碼可以正常工作,除了警告。

這是您的工作代碼:

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

使用此代碼可快速顯示警報視圖控制器。 它可能會幫助您。

 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;
     }

 }

暫無
暫無

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

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