UIAlertAction不显示已传递的消息

时间:2016-06-02 07:23:29

标签: ios uialertview nsnotificationcenter swift2.2

我能够在两个类之间成功传递一个消息字符串,但我的UIAlertAction没有显示消息。

代码发送消息

var message = String()

Alamofire.request(.POST, endPoint, headers: Auth_header, parameters: parameters, encoding: .JSON)
        .validate()
        .responseJSON {
        response in

        switch response.result {
        case .Success(let data):
            let value = JSON(data)
            if value["message"].string != nil {
                message = String(value["message"])
                let dic = ["message": message]
                print("hello")
                NSNotificationCenter.defaultCenter().postNotificationName("notification",object: nil, userInfo: dic)
            }

            onCompletion()

        case .Failure(let error):
            print("Request failed with error: \(error)")
            onError?(error)
        }

代码接收和显示消息

import UIKit

class TaskDetailsViewController: UIViewController {


@IBAction func submitBtn(sender: AnyObject) {
     loadTasks()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TaskDetailsViewController.displayMessage(_:)), name: "notification", object: nil)

}

func displayMessage(notification: NSNotification) {

    if let message = notification.userInfo!["message"]{

        //initialize Alert Controller
        let alertController = UIAlertController(title: "Success", message: message.string, preferredStyle: .Alert)
        print(message)
        print("world")
        //Initialize Actions
        let okAction = UIAlertAction(title: "Ok", style: .Default){
            (action) -> Void in
            self.dismissViewControllerAnimated(true, completion: nil)
        }

        //Add Actions
        alertController.addAction(okAction)

        //Present Alert Controller
        self.presentViewController(alertController, animated: true, completion: nil)

    }
}

我的打印

hello
  Score created.
world

Output not shown

4 个答案:

答案 0 :(得分:1)

我认为message.string传递给nilUIAlertControllermessage。检查两次。

在获得之后打印bin,以便了解您所获得的内容。

您也可以设置断点来检查您是否收到数据。

答案 1 :(得分:1)

原来我只需要在displayMessage函数中将message.string更改为message as? String

答案 2 :(得分:0)

成功阻止:

case .Success(let data):
        let value = JSON(data)
        if let message = value["message"] as? String {  
            print("message")             
            let dic = ["message": message]
            NSNotificationCenter.defaultCenter().postNotificationName("notification",object: nil, userInfo: dic)
        }

在警报控制器演示中:

if let message = notification.userInfo!["message"] as? String {

    //initialize Alert Controller
    let alertController = UIAlertController(title: "Success", message: message, preferredStyle: .Alert)
    print(message)
    print("world")
    //Initialize Actions
    let okAction = UIAlertAction(title: "Ok", style: .Default){
        (action) -> Void in
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    //Add Actions
    alertController.addAction(okAction)

    //Present Alert Controller
    self.presentViewController(alertController, animated: true, completion: nil)

}

如果它没有出现问题,则消息字符串为nil或消息字符串为空字符串(“”)。

答案 3 :(得分:0)

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TaskDetailsViewController.displayMessage(_:)), name: "notification", object: nil)必须在func displayMessage(notification: NSNotification)之内:

func displayMessage(notification: NSNotification){
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(TaskDetailsViewController.displayMessage(_:)), name: "notification", object: nil)
// Your code
}

然后你删除了观察者:

deinit{
NSNotificationCenter.defaultCenter().removeObserver
}