Click here to Skip to main content
13,550,849 members
Click here to Skip to main content
Add your own
alternative version


5 bookmarked
Posted 13 Jun 2014
Licenced CPOL

Adding a Notification Message to an iOS App

, 13 Jun 2014
Rate this:
Please Sign up or sign in to vote.
How to add an fade-in message to your iPhone app in 5 minutes


In this tip, I'm going to describe a useful little add-in that you can add to your XCode iPhone/iPad projects, which lets you easily display a popup message at the bottom of the screen.

It'll take you about 5 minutes to modify your XCode project to use this control, and once you've done it, you can display a notification message in any of your UIViewController screens using one line of code:

[self showStatusMessage:@"Loading Customer records.."];

That one line of code will make your message fade onto the screen, wait for 2 seconds, then fade it away again.

I know, it may sound pretty dumb, but in use, this is a really cool way just to let your users know that something has happened, without it "getting in the way" of your user-interface and without making your Objective-C code any more unreadable.

Screenshots don't really do this control justice. I often have functionality in my app where, say, a web service gets called, and it might take a few seconds for the response to come back.

With this control, I can easily let the user know when a service has successfully finished running, without having to specifically add a "status" UILabel to my screens, or display a modal "Record was successfully saved" HUD dialog.

With this control, I just need that one line of code, to unobtrusively show the user that their database record has been saved, their web service has been called, and so on.

This is my favourite iPhone control!

Adding the 'StatusPopup' UIView

Attached are the .m and .h files for the new StatusPopup control, which is the new UIView which gets faded onto the screen.

It contains just one function, which takes your string, measures it (to work out how large the UIView needs to be), and adds a drop-shadow and a border to the UIView.

I've chosen to position the UIView horizontally-aligned and 20 pixels from the bottom of the screen, but you can, of course, change this if you want.

It can also cope with very long strings, wrapping them onto several lines, but just remember, your users will only get 2 seconds to read the message!

When I wrote this class, my first instincts were to just add this UIView to one particular screen. I would add a new variable (@property) to the screen's .h file, then, when I wanted to display the popup, I would initialise it, and call its "doTheAnimation" member function:

So, my .h file would contain something like this:

#import "StatusPopup.h"

@interface LoginViewController

@property (strong, nonatomic) StatusPopup* statusPopup;


And my .m file would contain this...

@synthesize statusPopup;

    //  (Code to save some data to the database)

    . . .

    self.statusPopup = [[StatusPopup alloc] init];
    [self.statusPopup doTheAnimation:self.view labelText:@"Successfully saved."];

Yup, absolutely nothing wrong with that.

Except, I then found myself repeating this code in my next UIViewController. And the next. And the next.

And then found I needed extra code - what happens if I wanted to show a new status message, but there was already one on the screen? Suddenly, I needed to add fixes in each version of this code.


As this is a control which would be useful in most of my iPhone screens, I decided to write a Class Extension to the UIViewController class, so that all of my screens could easily use this control.

And this is where Objective-C categories come in.

What we can do is to add this StatusPopup control to all of our UIViewController screens in one go, then add a helper function, to look after displaying the message for us.

The UIViewHelper.h and UIViewHelper.m files do this for us.

The .h file merely adds a new showStatusMessage function to the existing UIViewController class, along with a StatusPopup variable, as described above. So, the .h file looks like this:

#import <UIKit/UIKit.h>
#import "StatusPopup.h"

@interface UIViewController (StatusMessage)
@property (strong, nonatomic) StatusPopup* statusPopup;

Now, in categories, you cannot use the synthesize command. So we can't use this line in our .m file:

@synthesize statusPopup;    // This will throw an error!

If you were to try this, XCode will throw up this error:

@synthesize not allowed in a category's implementation.

Instead, we need to go back to the runtime.h to allow us to load and save the value of our new property:

#import "UIViewHelper.h"
#import <objc/runtime.h>

@implementation UIViewController (StatusMessage)
@dynamic statusPopup;

NSString const *key = @"kKeyStatusPopup";

- (void)setStatusPopup:(StatusPopup*)test
    objc_setAssociatedObject(self, &key, (id)test, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

    return objc_getAssociatedObject(self, &key);

    if (self.statusPopup != nil)
        [self.statusPopup removeFromSuperview];
        self.statusPopup= nil;
    self.statusPopup = [[StatusPopup alloc] init];
    [self.statusPopup doTheAnimation:self.view labelText:statusMessage];


(My thanks to this article for showing me how to do this !)

As you can see, other than getting and setting the StatusPopup value, there's really not much to this UIViewController class extension, just our new showStatusMessage function.

Adding the Code to your Project

I said earlier that adding this popup to your code would take you about 5 minutes.

All you have to do is copy the four files into your project - I've chosen to add mine into a Helpers folder...

...then select your project's .pch file, and add one line of code:

#import "UIViewHelper.h"

And that's it.

After building your project, you can go into any of your UIViewController screens, and use the new showStatusMessage function. XCode will even show you its parameters:

It doesn't get much easier than that!

Final Thoughts

Please leave some feedback if you find any issues using this code, and don't forget to leave a rating if you find this code useful.

You can also find some other useful walkthroughs on my blog:

Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Michael Gledhill
Software Developer Partner Reinsurance
Switzerland Switzerland
I'm a software developer, working with Visual Studio and XCode, working for a financial company.

Author of the PartnerReSearch iPad app, winner of a "Business Insurance Innovation Award" in 2013, and a TechAward2014 "Innovation of the year" award in 2014.

Living near Zurich, Switzerland, with my Swiss-English family.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 13 Jun 2014
Article Copyright 2014 by Michael Gledhill
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid