Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialBrian Patterson
19,588 PointsAlertView.
I am trying to add an alert view in the application but this seems to be deprecated . How would you add an UIAlertViewController in the project?
5 Answers
Brian Patterson
19,588 PointsWhere do I add the above code? Network Operation or the ViewController ?
Beatriz Macedo
9,903 PointsThat depends, do you want this message to appear as a result of some action on the app, or as a result of a network operation? You should add it to where the action that makes the alert appear is.
Brian Patterson
19,588 PointsCan you have it in both and how would you implement it?
Brian Patterson
19,588 PointsIf I put it in the Network Operation I get unresolved identifier errors. import Foundation
class NetworkOperation {
lazy var config: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
lazy var session: NSURLSession = NSURLSession(configuration: self.config)
let queryURL: NSURL
typealias JSONDictionaryCompletion = ([String: AnyObject]? -> Void)
init(url: NSURL) {
self.queryURL = url
}
func downloadJSONFromURL(completion: JSONDictionaryCompletion) {
let request = NSURLRequest(URL: queryURL)
let dataTask = session.dataTaskWithRequest(request) {
(let data, let response, let error) in
// 1. Check HTTP response for successful GET request
if let httpResponse = response as? NSHTTPURLResponse {
switch httpResponse.statusCode {
case 200:
// 2. Create JSON object with data
let jsonDictionary = NSJSONSerialization.JSONObjectWithData(data, options: nil, error: nil) as? [String: AnyObject]
completion(jsonDictionary)
default:
println("GET request not successful. HTTP status code: \(httpResponse.statusCode)")
}
} else {
println("Error: Not a valid HTTP response")
// Create the alert
var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert)
// Add Back button to alert with no real action, just to close the alert dialog
alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default, handler: nil)) // Show the alert
self.presentViewController(alert, animated: true, completion: nil)
}
}
dataTask.resume()
}
}
Beatriz Macedo
9,903 PointsYou should add an import to UIKit as UIAlertController is from UIKit. Just add this line of code to the top of your code below import Foundation:
import UIKit
Brian Patterson
19,588 PointsThanks for that Beatriz. Although, I am running into a problem. The following line of code tells me that I have a " 'NetworkOperation' does not have a member named 'presentViewController'" Please see below.
self.presentViewController(alert, animated: true, completion: nil)
Brian Patterson
19,588 PointsIs this because I need to add this into the viewController? If so how?
Beatriz Macedo
9,903 PointsBeatriz Macedo
9,903 PointsHi Brian, I would add an alert message this way:
// Create the alert var alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: UIAlertControllerStyle.Alert) // Add Back button to alert with no real action, just to close the alert dialog alert.addAction(UIAlertAction(title: "Back", style: UIAlertActionStyle.Default, handler: nil)) // Show the alert self.presentViewController(alert, animated: true, completion: nil)
Let me know if this works for you :D