Custom UIAlertController – iOS (Objective-C)


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.

- (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.

    //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.

    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.

    [controller addTextFieldWithConfigurationHandler:^(UITextField *textField) {}];

And you can customize it too with…

    [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.

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.

//
//  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)

Leave a comment

Your email address will not be published. Required fields are marked *