Writing handler for UIAlertAction

Instead of self in your handler, put (alert: UIAlertAction!). This should make your code look like this alert.addAction(UIAlertAction(title: “Okay”, style: UIAlertActionStyle.Default, handler: {(alert: UIAlertAction!) in println(“Foo”)})) this is the proper way to define handlers in Swift. As Brian pointed out below, there are also easier ways to define these handlers. Using his methods is discussed … Read more

UIAlertView first deprecated IOS 9

From iOS8 Apple provide new UIAlertController class which you can use instead of UIAlertView which is now deprecated, it is also stated in deprecation message: UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead So you should use something like this UIAlertController * alert = [UIAlertController alertControllerWithTitle:@”Title” message:@”Message” preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction* yesButton = [UIAlertAction … Read more

Presenting a UIAlertController properly on an iPad using iOS 8

You can present a UIAlertController from a popover by using UIPopoverPresentationController. In Obj-C: UIViewController *self; // code assumes you’re in a view controller UIButton *button; // the button you want to show the popup sheet from UIAlertController *alertController; UIAlertAction *destroyAction; UIAlertAction *otherAction; alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; destroyAction = [UIAlertAction actionWithTitle:@”Remove All Data” style:UIAlertActionStyleDestructive … Read more

UIAlertController – add custom views to actionsheet

UIAlertController extends UIViewController, which has a view property. You can add subviews to that view to your heart’s desire. The only trouble is sizing the alert controller properly. You could do something like this, but this could easily break the next time Apple adjusts the design of UIAlertController. Swift 3 let alertController = UIAlertController(title: “\n\n\n\n\n\n”, … Read more

Add Image to UIAlertAction in UIAlertController

And that’s how it’s done: let image = UIImage(named: “myImage”) var action = UIAlertAction(title: “title”, style: .default, handler: nil) action.setValue(image, forKey: “image”) alert.addAction(action) the image property is not exposed, so there’s no guarantee of this working in future releases, but works fine as of now

How would I create a UIAlertView in Swift?

From the UIAlertView class: // UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead On iOS 8, you can do this: let alert = UIAlertController(title: “Alert”, message: “Message”, preferredStyle: UIAlertControllerStyle.Alert) alert.addAction(UIAlertAction(title: “Click”, style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(alert, animated: true, completion: nil) Now UIAlertController is a single class for creating and interacting with what … Read more

UIAlertController custom font, size, color

Not sure if this is against private APIs/properties but using KVC works for me on ios8 UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@”Dont care what goes here, since we’re about to change below” message:@”” preferredStyle:UIAlertControllerStyleActionSheet]; NSMutableAttributedString *hogan = [[NSMutableAttributedString alloc] initWithString:@”Presenting the great… Hulk Hogan!”]; [hogan addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:50.0] range:NSMakeRange(24, 11)]; [alertVC setValue:hogan forKey:@”attributedTitle”]; UIAlertAction *button = … Read more