2016-02-19 73 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

相關問題