按下按钮时iOS表视图冻结,控制台

时间:2016-06-03 23:22:19

标签: ios swift ios9

我有一个视图,其中显示了自定义单元格,当我按下单元格中的按钮时,整个应用程序冻结,我必须强制关闭它并重新启动它。当应用程序冻结时,控制台中没有显示任何内容,我也不知道如何调试它。我也尝试过使用非自定义按钮。

Before hitting button

Frozen Screen

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//delete button

@IBAction func deleteButton(sender: AnyObject) {

if phonee == "0"{
   let URL = "BLURRED FOR PRIVACY"

    var url : NSString = URL
    var urlStr : NSString = url.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
    var searchURL : NSURL = NSURL(string: urlStr as String)!
    print (searchURL)
    let task = NSURLSession.sharedSession().dataTaskWithURL(searchURL, completionHandler: { (data, response, error) -> Void in
        var urlContent = NSString(data: data!, encoding: NSASCIIStringEncoding) as NSString!
        print(urlContent)

        if urlContent.localizedStandardContainsString("Removed event with ID") {
          dispatch_sync(dispatch_get_main_queue()) {
            var settings = Modal.Settings()
            settings.bodyColor = .whiteColor()
            Modal(title: "Success", body: "Task deleted, refresh the page.", status: .Success, settings: settings).show()
            }

            }else{
            dispatch_sync(dispatch_get_main_queue()) {
            var settings = Modal.Settings()
            settings.bodyColor = .whiteColor()
            Modal(title: "Error", body: "Are you connected to the internet?", status: .Error, settings: settings).show()
            }
        }
    })
    task.resume()

}else{
    dispatch_sync(dispatch_get_main_queue()) {
    var settings = Modal.Settings()
    settings.bodyColor = .whiteColor()
    Modal(title: "Error", body: "You can't delete an accepted task", status: .Error, settings: settings).show()
    }
    }
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///// //付费按钮

@IBAction func payButton(sender: AnyObject) {

    if phonee == "0"{

        dispatch_sync(dispatch_get_main_queue()) {
        var settings = Modal.Settings()
        settings.bodyColor = .whiteColor()
        Modal(title: "Error", body: "Please wait for the task to be accepted", status: .Error, settings: settings).show()
        }
        }else{

        url = url + userID + "&event_id=" + eventID
        let myURL = NSURL(string: url)!

        print (myURL)

        let task = NSURLSession.sharedSession().dataTaskWithURL(myURL, completionHandler: { (data, response, error) -> Void in
            var urlContent = NSString(data: data!, encoding: NSASCIIStringEncoding) as NSString!
            print(urlContent)
        })
        task.resume()

        progressBar.progress = 1.0
        complete1.textColor = UIColor(red: 43/255.0, green: 192/255.0, blue: 228/255.0, alpha: 1.0)
        accepted1.textColor = UIColor.blackColor()

        reportButton.userInteractionEnabled = false
        deleteButton.userInteractionEnabled = false
        contactButton.userInteractionEnabled = false
        payButton.userInteractionEnabled = false
        priceLabel.textColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
        self.deleteButton.backgroundColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
        self.contactButton.backgroundColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
        self.payButton.backgroundColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
        priceLabel.textColor = UIColor(red: 170/255, green: 170/255, blue: 170/255, alpha: 1.0)
        reportButton.setImage(UIImage(named: "nation2.png"), forState: UIControlState.Normal)


        }
}

/////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///// //联系按钮

@IBAction func contactButton(sender: AnyObject) {

    if phonee == "0"{
        dispatch_sync(dispatch_get_main_queue()) {
        var settings = Modal.Settings()
        settings.bodyColor = .whiteColor()
        Modal(title: "Error", body: "Please wait for the task to be accepted", status: .Error, settings: settings).show()
        }
    }else{

        if let url = NSURL(string: "tel://\(phonee)") {
            UIApplication.sharedApplication().openURL(url)
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您正在从主线程调用dispatch_sync(dispatch_get_main_queue()),这会导致死锁。总是在主线程上调用IBActions(除非你是从另一个线程手动调用它们),因为所有用户输入都在主线程上处理,因此不需要这样做。