Custom UIAlertController – iOS (Objective-C)
Creating a custom UIAlertController helps you to show a popup in your iOS application whenever you need to show a simple warning/information popup. It is an embedded iOS library and that means you do not need to install any 3rd party library.
A classic popup in mobile applications contains 3 parts: title, content and buttons. You set title according the purpose of the popup, set content as message and set buttons to get action.
Create a Screen
Open your project, create a new and empty ViewController and add a simple button to trigger the popup.
Connect this button to ViewController.m file as IBOutlet by selecting “Assistant Editor” at top-left. Drag button with right-click from view to ViewController.m file.
Create a Simple AlertController
Then, create a simple UIAlertController with a title, a message and a button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- (IBAction)dialogButton:(id)sender { UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"AlertController Title" message:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pellentesque ullamcorper sollicitudin." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *buttonOk = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self actionOk]; }]; [controller addAction:buttonOk]; [self presentViewController:controller animated:YES completion:nil]; } -(void) actionOk { //your "ok" action here... } |
If you want to add another button, for example a cancel button, just add it like “Ok” button.
1 2 3 4 5 6 |
//UIAlertActionStyleDestructive gives you a red colored button UIAlertAction *buttonCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { }]; [controller addAction:buttonCancel]; |
Add More Buttons and TextField
You can add more than 2 buttons to your AlertController by repeating add action method.
1 2 3 4 5 6 7 8 9 10 |
UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Button One" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self actionOne]; }]; UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Button Two" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self actionTwo]; }]; [controller addAction:button1]; [controller addAction:button2]; |
This is a basic view of UIAlertController in an iOS application. You can customize it, of course. For example, you can change colors, fonts, size or you can add a text field in your popup. Let’s start with adding a text field to our popup. In iOS, UIAlertController has a method to add a textfield into the window and it is very simple as a line of code.
1 |
[controller addTextFieldWithConfigurationHandler:^(UITextField *textField) {}]; |
And you can customize it too with…
1 2 3 4 5 |
[controller addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"PlaceHolder"; textField.textColor = [UIColor blueColor]; textField.keyboardType = UIKeyboardTypeDefault; }]; |
Customize AlertController
As the last customizing, lets change colors of some views in this popup.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
NSMutableAttributedString *titleTxt = [[NSMutableAttributedString alloc] initWithString:@"AlertController Title"]; [titleTxt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22.0] range:NSMakeRange(0, titleTxt.length)]; [titleTxt addAttribute:NSForegroundColorAttributeName value:UIColor.brownColor range:NSMakeRange(0, 15)]; [titleTxt addAttribute:NSForegroundColorAttributeName value:UIColor.purpleColor range:NSMakeRange(16, 5)]; [controller setValue:titleTxt forKey:@"attributedTitle"]; controller.view.tintColor = UIColor.darkGrayColor; [controller addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"PlaceHolder"; textField.textColor = [UIColor blueColor]; textField.keyboardType = UIKeyboardTypeDefault; }]; |
That’s all. Maybe it is not a good idea to change all colors in popup like this example 🙂 Be certain of your colors and app theme in a harmony.
Final Work: Custom AlertController
To get the latest version of popup in this tutorial, see the code below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
// // ViewController.m // HelloWorld // // Created by Kağan Kartal on 11.11.2018. // Copyright © 2018 CoffeeBreakCodes. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (IBAction)dialogButton:(id)sender { UIAlertController *controller = [UIAlertController alertControllerWithTitle:@"AlertController Title" message:@"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed pellentesque ullamcorper sollicitudin." preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *button1 = [UIAlertAction actionWithTitle:@"Button One" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self actionOne]; }]; UIAlertAction *button2 = [UIAlertAction actionWithTitle:@"Button Two" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self actionTwo]; }]; //UIAlertActionStyleDestructive gives you a red colored button UIAlertAction *buttonCancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) { }]; NSMutableAttributedString *titleTxt = [[NSMutableAttributedString alloc] initWithString:@"AlertController Title"]; [titleTxt addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:22.0] range:NSMakeRange(0, titleTxt.length)]; [titleTxt addAttribute:NSForegroundColorAttributeName value:UIColor.brownColor range:NSMakeRange(0, 15)]; [titleTxt addAttribute:NSForegroundColorAttributeName value:UIColor.purpleColor range:NSMakeRange(16, 5)]; [controller setValue:titleTxt forKey:@"attributedTitle"]; controller.view.tintColor = UIColor.darkGrayColor; [controller addTextFieldWithConfigurationHandler:^(UITextField *textField) { textField.placeholder = @"PlaceHolder"; textField.textColor = [UIColor blueColor]; textField.keyboardType = UIKeyboardTypeDefault; }]; [controller addAction:button1]; [controller addAction:button2]; [controller addAction:buttonCancel]; [self presentViewController:controller animated:YES completion:nil]; } -(void) actionOne { } -(void) actionTwo { } @end |
©Coffee Break Codes – Custom Alert Controller – iOS (Objective-C)