2016-02-19 72 views
1

我正在使用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) 
} 

现在,当我运行它时,网络通信正常发生,但然后我得到一个错误,警报窗口从不出现。

试图同时它解除分配是不允许的,可能会导致不确定的行为

+0

alertView的所有声明你是哪里试图呈现呢? – dan

+0

在UIViewController中 – skrite

回答

0

只需添加这一个行代码的顶部做一个全局请求加载视图控制器的看法UIAlertController。

由于在swift中我们不必释放任何视图。斯威夫特的语言让他们站在一边。

let alertView : UIAlertController? 

撤除类

编辑

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) 
} 
+0

我该怎么做如果我必须实例化它的属性? – skrite

+0

@skrite现在试试这个代码 –

+0

真棒!非常感谢 ! – skrite

相关问题