58th Day As An iOS Developer With No Experience

58th Day -  November 24, 2015

TUESDAY - Two things I wanna share today. These are the things that I've learned today. First is how to share two or more AlertViews in one delegate. Second is the observer to check if the app is returned from the background.

I would like to discuss the first one. Sharing UIAlertViews in one delegate. I assume you know that UIAlertView is already deprecated. Let's get started anyway, I'll give you one scenario. You have two or more UIAlertViews that are sharing delegates, how will you know or how will your program know which of which should do certain things? The answer to that would be adding tags.

See the code below:

#define kAlertViewOne 1
#define kAlertViewTwo 2

UIAlertView *yourUIAlertView1 = [[UIAlertView alloc] init...
yourUIAlertView1.tag = kAlertViewOne;

UIAlertView *yourUIAlertView2 = [[UIAlertView alloc] init...
yourUIAlertView2.tag = kAlertViewTwo;

Next, we need to separate now the tasks of each alertViews since they will of course share the same delegates. I can't think of another way than this.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == kAlertViewOne) {
        // TODO: tasks for alerview1
    } else if(alertView.tag == kAlertViewTwo) {
        // TODO: tasks for alerview2

Ok. We're done here. Last, I would like to share the code for observing if our app has just returned from the background. Take a look:

// observer checks if we're back from the background
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMethod) name:UIApplicationWillEnterForegroundNotification object:nil];

This code above has been helpful to my project. Of course you need to edit the selector, that's no brainer. If this post helped you, please do share and subscribe.

Post a Comment