我现在有一个简单的应用程序,我有一个名为" Load"的按钮,我有以下几行。每次运行它时,我点击加载按钮来加载细节,当我这样做时,它似乎冻结并导致线程1中的SIGABRT,然后调出AppDelegate文件。我怎么能阻止这个?怎么了?
编辑:我已经缩小到方法本身,因为当我创建它所以它使用ViewDidLoad中没有按钮的方法时它会在我点击任何东西之前导致异常。还是,为什么?
loadButton.target = self
loadButton.action = "getMovieDetails:353"
函数getMovieDetails
// REST Request to get movie details
func getMovieDetails(movieID: Int) {
let url: NSURL = NSURL(string: "http://yts.re/api/movie.json?id=\(movieID)")
var request: NSMutableURLRequest = NSMutableURLRequest()
request.URL = url
request.HTTPMethod = "GET"
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue(), completionHandler:{ (response:NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var error: AutoreleasingUnsafePointer<NSError?> = nil
let json: NSDictionary! = NSJSONSerialization.JSONObjectWithData(data, options:NSJSONReadingOptions.MutableContainers, error: error) as? NSDictionary
if json {
let movieTitleClean: AnyObject! = json["MovieTitleClean"]
self.navigationItem.title = "\(movieTitleClean)"
} else {
self.navigationItem.title = "Error title"
}
})
}
对appDelegate唯一改变的是第一种方法
application.setStatusBarHidden(false, animated: false)
application.setStatusBarStyle(UIStatusBarStyle.LightContent, animated: false)
答案 0 :(得分:0)
您的选择器对于loadButton.action无效。你最后不能像353
那样给出参数。触摸按钮时,参数将是按钮本身,因此您应该具有以下签名的功能:
func handleLoadButtonTouch(loadButton: UIButton) { ... }
然后,按如下方式设置操作:
loadButton.action = "handleLoadButtonTouch:" // note, the ':' is a weird artifact from Obj-C, which simply means 1 argument lives there