Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save AlexHedley/471008b5ce793164c79b26784178d175 to your computer and use it in GitHub Desktop.

Select an option

Save AlexHedley/471008b5ce793164c79b26784178d175 to your computer and use it in GitHub Desktop.
Simple category that wraps around the UIAlertController annoying stuff
//
// UIAlertController+Utilities.h
//
// Created by valvoline on 18/12/14.
// Copyright (c) 2014 sofapps. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIAlertController (Utilities)
+ (UIAlertController*)alertControllerWithTitle:(NSString *)aTitle
message:(NSString *)aMessage
actions:(UIAlertAction *)cancelAct,...;
+ (UIAlertController*)sheetControllerWithTitle:(NSString *)aTitle
message:(NSString *)aMessage
actions:(UIAlertAction *)cancelAct, ...;
@end
//
// UIAlertController+Utilities.m
//
// Created by valvoline on 18/12/14.
// Copyright (c) 2014 sofapps. All rights reserved.
//
#import "UIAlertController+Utilities.h"
@implementation UIAlertController (Utilities)
+ (UIAlertController*)alertControllerWithTitle:(NSString *)aTitle
message:(NSString *)aMessage
actions:(UIAlertAction *)cancelAct, ...
{
va_list args;
va_start(args, cancelAct);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:aTitle
message:aMessage
preferredStyle:UIAlertControllerStyleAlert];
for (UIAlertAction *anAct = cancelAct; anAct != nil; anAct = va_arg(args, UIAlertAction*))
{
[alert addAction:anAct];
}
va_end(args);
return alert;
}
+ (UIAlertController*)sheetControllerWithTitle:(NSString *)aTitle
message:(NSString *)aMessage
actions:(UIAlertAction *)cancelAct, ...
{
va_list args;
va_start(args, cancelAct);
UIAlertController *alert = [UIAlertController alertControllerWithTitle:aTitle
message:aMessage
preferredStyle:UIAlertControllerStyleActionSheet];
for (UIAlertAction *anAct = cancelAct; anAct != nil; anAct = va_arg(args, UIAlertAction*))
{
[alert addAction:anAct];
}
va_end(args);
return alert;
}
@end
//http://stackoverflow.com/a/30941356/2895831
// need local variable for TextField to prevent retain cycle of Alert otherwise UIWindow
// would not disappear after the Alert was dismissed
__block UITextField *localTextField;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Global Alert" message:@"Enter some text" preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
NSLog(@"do something with text:%@", localTextField.text);
// do NOT use alert.textfields or otherwise reference the alert in the block. Will cause retain cycle
}]];
[alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
localTextField = textField;
}];
[alert show];
//https://github.com/agilityvision/FFGlobalAlertController
//http://stackoverflow.com/a/30941356/2895831
#import "UIAlertController+Window.h"
#import <objc/runtime.h>
@interface UIAlertController (Window)
- (void)show;
- (void)show:(BOOL)animated;
@end
@interface UIAlertController (Private)
@property (nonatomic, strong) UIWindow *alertWindow;
@end
@implementation UIAlertController (Private)
@dynamic alertWindow;
- (void)setAlertWindow:(UIWindow *)alertWindow {
objc_setAssociatedObject(self, @selector(alertWindow), alertWindow, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIWindow *)alertWindow {
return objc_getAssociatedObject(self, @selector(alertWindow));
}
@end
@implementation UIAlertController (Window)
- (void)show {
[self show:YES];
}
- (void)show:(BOOL)animated {
self.alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.alertWindow.rootViewController = [[UIViewController alloc] init];
id<UIApplicationDelegate> delegate = [UIApplication sharedApplication].delegate;
// Applications that does not load with UIMainStoryboardFile might not have a window property:
if ([delegate respondsToSelector:@selector(window)]) {
// we inherit the main window's tintColor
self.alertWindow.tintColor = delegate.window.tintColor;
}
// window level is above the top window (this makes the alert, if it's a sheet, show over the keyboard)
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
self.alertWindow.windowLevel = topWindow.windowLevel + 1;
[self.alertWindow makeKeyAndVisible];
[self.alertWindow.rootViewController presentViewController:self animated:animated completion:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// precaution to insure window gets destroyed
self.alertWindow.hidden = YES;
self.alertWindow = nil;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment