我想问有关新 UIAlertController 的问题。如何检测自定义类上的错误以向用户显示警报 View ?我想在 switch case 变为默认语句时执行警报 View 。我的 NetworkOperation 类是带有闭包的自定义类,用于从 Web 下载一些 JSON 数据。
class NetworkOperation {
lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let mainURL: NSURL
typealias JSONWeatherDataDictionary = ([String: AnyObject]?) -> Void
init(url: NSURL) {
mainURL = url
}
func downloadWeatherDataFromWeb(completion: JSONWeatherDataDictionary) {
let request = NSURLRequest(URL: mainURL)
let dataTask = session.dataTaskWithRequest(request) {
(let data, response, error) in
if let httpResponse = response as? NSHTTPURLResponse {
switch httpResponse.statusCode {
case 200:
// HTTP Response success use the weather data !
do {
let weatherDataDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? [String: AnyObject]
completion(weatherDataDictionary)
} catch {
print("Data can't get")
}
default:
// I want to execute my alert view in there. showAlertView()
print("Http Response failed with this code: \(httpResponse.statusCode)")
}
} else {
print("HTTP Response convert fail !")
}
}
dataTask.resume()
}
}
如果执行默认情况,我如何在 View Controller 中看到我的警报 View ?我尝试使用 UIAlertViewDelegate 解决这个问题,但它在 iOS9 中已弃用,所以我想学习解决这个问题的最佳常用方法。 谢谢大家...
最佳答案
如果您只想显示一个警报,让用户知道错误,并提供一个取消按钮来关闭该按钮,您可以使用 UIAlertController
.
我使用 HTTP URL 测试了以下代码,导致其失败,弹出警报 View ,显示 status code
;点击“取消”按钮后,它会自行消失。
func downloadWeatherDataFromWeb(completion: JSONWeatherDataDictionary)
{
let request = NSURLRequest(URL: mainURL)
let dataTask = session.dataTaskWithRequest(request)
{
(let data, response, error) in
if let httpResponse = response as? NSHTTPURLResponse
{
switch httpResponse.statusCode
{
case 200:
do
{
let weatherDataDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: .MutableContainers) as? [String: AnyObject]
completion(weatherDataDictionary)
}
catch
{
print("Could not retrieve data")
}
default:
print("Http Response failed with the following code: \(httpResponse.statusCode)")
let alert = UIAlertController(title: "HTTP Error", message:String(httpResponse.statusCode), preferredStyle: UIAlertControllerStyle.Alert)
//set up cancel action
let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Cancel)
{
action -> Void in
//should you want to carry some other operations upon canceling
}
//add the action
alert.addAction(cancelAction)
dispatch_async(dispatch_get_main_queue(),
{
self.presentViewController(alert, animated:true, completion:
{
//alert controller presented
})
})
}
}
else
{
print("HTTP Response conversion failed!")
}
}
dataTask.resume()
}
更新:回应您的评论
请添加UIViewController
变量:
class NetworkOperation
{
var viewController : UIViewController = UIViewController()
....
并修改default:
以上案例:
dispatch_async(dispatch_get_main_queue(),
{
self.addChildViewController(self.viewController)
self.view.addSubview(self.viewController.view)
self.viewController.didMoveToParentViewController(self)
self.viewController.presentViewController(alert, animated:true,
completion:
{
//alert controller presented
})
})
我一分钟前才冒昧测试一下;它会弹出并关闭。
另一个更新:
然后,我们可以执行以下操作:
dispatch_async(dispatch_get_main_queue(),
{
let topViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
topViewController!.presentViewController(alert, animated:true,
completion:
{
//alert controller presented
})
})
关于ios - 在 VC 上显示 AlertView。当自定义类捕获闭包错误时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32177021/