While developing an iOS app, usually you have to show an alert view. The code to show it is very simple, but as most of the UKit components, it works with the delegate pattern.

Working with delegates is easy, but the resulting code is split between two parts. The part where the component is initialized, and the methods of the delegate.

To try to improve that, last week I developed my first CocoaPods library, STAlertView. The idea is simple, have a native UIAlertView which works with blocks of code instead of delegate.

Using STAlertView you can set two blocks of code at the same place you declare the title and message. With this blocks of code you can define the behavior of the buttons. The best way to understand it is to compare two similar examples, the first with a UIAlertView and the second one with a STAlertView.

UIAlertView

With the native alert view, the code to show two alert views at the same view controller has this structure:

-(void) viewDidLoad
{
   ...
  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert view 1"
                       message:@"Message of alert view 1"
                       delegate:self
                       cancelButtonTitle:@"Cancel"
                       otherButtonTitles:@"Ok", nil];
  alertView.tag = 1;
  [alertView show];
   ...
}
...
// And the other one in another place
...
 
-(void) showAnotherAlert
{
   ...
  UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert view 2"
                       message:@"Message of alert view 2"
                       delegate:self
                       cancelButtonTitle:@"Cancel"
                       otherButtonTitles:@"Ok", nil];
  alertView.tag = 2;
  [alertView show];
   ...
}
 
...
// In another place of the view controller
...
 
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
   if(alertView.tag == 1){
       if (buttonIndex == 0 )
              // Cancel button of AlertView 1
       else if (buttonIndex == 1)
              // Ok button of AlertView 1
 
    } else if(alertView.tag == 2){
 
       if (buttonIndex == 0 )
              // Cancel button of AlertView 2
       else if (buttonIndex == 1)
              // Ok button of AlertView 2
    }
}

If the same view controller has more than one alert view, then, you have to use the tag to difference and do the stuff you need to do.

 

STAlertView

With this component, you will be able to set the behaviour code of the buttons at the same place you initialize the alert view. The same previous example with STAlertView would be:

-(void) viewDidLoad
{
    ...
    [[STAlertView alloc] initWithTitle:@"Title of the alert"
          message:@"Message you want to show"
          cancelButtonTitle:@"No" 
          otherButtonTitles:@"Yes"
          cancelButtonBlock:^{
                  // Code todo when the user cancel
                  ...
          } otherButtonBlock:^{
                  // Code todo when the user accept
                  ...
          }];
    ...
}
 
...
 
-(void) showAnotherAlert
{
    ...
    STAlertView *alertView = [[STAlertView alloc] initWithTitle:@"Title of the alert"
          message:@"Message you want to show"
          cancelButtonTitle:@"No" 
          otherButtonTitles:@"Yes"
          cancelButtonBlock:^{
                  // Code todo when the user cancel
                  ...
          } otherButtonBlock:^{
                  // Code todo when the user accept
                  ...
          }];
    ...
}

 

Conclusion

I think that the native UIAlertView is quite simple and easy to use, but the resulting code (especially if  you have more than one) is complicate to read.

With the component I developed (https://github.com/nmaletm/STAlertView), you can improve the readability of your code. And it is very easy to install, you have to add it to your Podfile:

pod "STAlertView"

And include it at the view controller:

#import <STAlertView/STAlertView.h>

About Néstor Malet
I’m a web and iOS developer. I like to start projects, personal ones, with friends or at work.

Leave a Reply

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