【问题标题】:deallocating is not allowed when attempting to load the view尝试加载视图时不允许解除分配
【发布时间】:2016-02-19 17:36:33
【问题描述】:

我正在使用 swift 向我的服务器发送消息,但是,当它结束时,我无法获得弹出警报。这是代码。

func sendSimpleCommand(siteId: Int, command: String) -> Int {
    Alamofire.request(.GET, commandUrl, parameters: ["site": siteId, "command": command, "device": "ios"])
        .responseJSON { response in
            //print(response.result)   // result of response serialization

            switch response.result {
            case .Success(_):
                print("success code back from api server for command sent")
                let alertView = UIAlertController(title: "Command Sent", message: "Your \(command) has been sent.", preferredStyle: .Alert)
                let alertAction = UIAlertAction(title: "OK", style: .Default) { _ in
                }
                alertView.addAction(alertAction)
            case .Failure(_):
                print("FAIL code back from api server for command sent")
                let alertView = UIAlertController(title: "Connect Error", message: "Network error, please try again", preferredStyle: .Alert)
                let alertAction = UIAlertAction(title: "OK", style: .Default) { _ in
                }
                alertView.addAction(alertAction)
            }
    }
    return 1
}




@IBAction func startButtonTouch(sender: UIButton) {
   let helper = HelperActions()
   let site = ActiveSite.sharedInstance.siteObject
   let command: String = "start"
   sendSimpleCommand(site.id , command: command)
}

现在,当我运行它时,网络通信正常进行,但随后出现错误并且警报窗口从未出现。

不允许在解除分配时尝试加载视图控制器的视图,这可能会导致未定义的行为

【问题讨论】:

  • 你想在哪里展示它?
  • 在 UIViewController 中

标签: ios swift uiviewcontroller


【解决方案1】:

只需在代码顶部添加这一行即可发出 UIAlertController 的全局请求。

与 swift 一样,我们不必释放任何视图。 Swift 语言在他们身边处理它。

let alertView : UIAlertController?

删除类中所有alertView的声明

编辑

func sendSimpleCommand(siteId: Int, command: String) -> Int {
Alamofire.request(.GET, commandUrl, parameters: ["site": siteId, "command": command, "device": "ios"])
    .responseJSON { response in
        //print(response.result)   // result of response serialization

        switch response.result {
        case .Success(_):
            print("success code back from api server for command sent")
            alertView = UIAlertController(title: "Command Sent", message: "Your \(command) has been sent.", preferredStyle: .Alert)
            let alertAction = UIAlertAction(title: "OK", style: .Default) { _ in
            }
            alertView.addAction(alertAction)
        case .Failure(_):
            print("FAIL code back from api server for command sent")

            alertView = UIAlertController(title: "Connect Error", message: "Network error, please try again", preferredStyle: .Alert)
            let alertAction = UIAlertAction(title: "OK", style: .Default) { _ in
            }
            alertView.addAction(alertAction)
        }
}
return 1
}




@IBAction func startButtonTouch(sender: UIButton) {
   let helper = HelperActions()
   let site = ActiveSite.sharedInstance.siteObject
   let command: String = "start"
   sendSimpleCommand(site.id , command: command)
}

【讨论】:

  • 如果我必须用属性实例化它,我该怎么做?
猜你喜欢
  • 1970-01-01
  • 2016-11-30
  • 1970-01-01
  • 2015-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多