Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios UIAlertAction未显示传递的消息_Ios_Uialertview_Nsnotificationcenter_Swift2.2 - Fatal编程技术网

Ios UIAlertAction未显示传递的消息

Ios UIAlertAction未显示传递的消息,ios,uialertview,nsnotificationcenter,swift2.2,Ios,Uialertview,Nsnotificationcenter,Swift2.2,我能够成功地在两个类之间传递字符串消息,但我的UIAlertAction没有显示消息 代码发送消息 var message = String() Alamofire.request(.POST, endPoint, headers: Auth_header, parameters: parameters, encoding: .JSON) .validate() .responseJSON { response in switc

我能够成功地在两个类之间传递字符串消息,但我的
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)

    }
}
接收和显示消息的代码

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

我认为当传递给
UIAlertController
时,您的
消息.string
nil
。检查两次

获取信息后打印
信息
,以便了解其中的内容


您还可以设置断点来检查是否正在获取数据。

结果表明,我只需要将
message.string
更改为
message as?成功块中我的displayMessage函数中的字符串

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)

}

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

NSNotificationCenter.defaultCenter().addObserver(self,selector:#selector(TaskDetailsViewController.displayMessage(35;:)、name:“notification”,object:nil)
必须位于
funct 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
}

什么意思?显示警报的方法是否存在问题,因为消息可以很好地传递