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 trial

iOS Build a Photo Browser iPhone App Gestures Introducing Gesture Recognizers

Pablo Alfaro
PLUS
Pablo Alfaro
Courses Plus Student 6,923 Points

UIAlertView deprecated in iOS8

In this video UIAlertView is used to present an alert to the user however that is now deprecated as of iOS 8. After searching Google I could not find answer as to how to present an UIAlertController from the custom THPhotoCell since it is not a view controller and it doesn't give us the option to present it using [self presentViewController:alert animated:YES completion:nil]; How can we go about presenting an UIAlertController to the user through the THPhotoCell?

4 Answers

Mahesh Babu
Mahesh Babu
3,402 Points

my like method

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Liked" message:nil preferredStyle:UIAlertControllerStyleAlert];

UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;

while (topController.presentedViewController) {
    topController = topController.presentedViewController;
}
[topController presentViewController:alert animated:YES completion:nil];

} Works 100 % Pablo Alfaro

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Literally first link in google after official reference: http://nshipster.com/uialertcontroller/

Pablo Alfaro
Pablo Alfaro
Courses Plus Student 6,923 Points

Yeah I have seen that page and bookmarked it for reference but on the PhotoCell.m file I am unable to call the UIAlertController using the [self presentViewController: animated: completion:] method because it is not a view controller. Were you able to call it?

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

UIAlertController is a subclass of UIViewController so you should be able to use it like this:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"You are still offline" message:@"Don't get desperate!" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
Pablo Alfaro
Pablo Alfaro
Courses Plus Student 6,923 Points

Yeah that is what I tried but get the error "No visible @interface for 'THPhotoCell' declares the selector 'presentViewController:animated:completion" Basically I'm getting the error because I am implementing the code in the PhotoCell.m file which is a subclass of UICollectionViewCell and is not a view controller. Did you add that code to your PhotoCell.m file?

alt text

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Ah the PhotoCell.m is not the View Controller! Well you need to call the UIAlertController from a view controller that contains the THPhotoCell as a subview or a root view, in your case it should be THPhotosViewController or the one that he uses to control the collection.

try this one:

  • (void)like { UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Liked" message:nil preferredStyle:UIAlertControllerStyleAlert];

    UINavigationController *navigationController = (UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController;

    UICollectionViewController *collectionView = (UICollectionViewController *)[navigationController visibleViewController];

    [collectionView presentViewController:alert animated:YES completion:nil];
    }