Skip to content

Instantly share code, notes, and snippets.

@logicaroma
Forked from tbalthazar/CustomAlertView.m
Created April 8, 2011 11:58
Show Gist options
  • Save logicaroma/909702 to your computer and use it in GitHub Desktop.
Save logicaroma/909702 to your computer and use it in GitHub Desktop.
// A small variant of Jeff Lamarche code as seen here : http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
//
// CustomAlertView.m
// Custom Alert View
//
// Created by jeff on 5/17/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "CustomAlertView.h"
#import "UIView-AlertAnimations.h"
#import <QuartzCore/QuartzCore.h>
@interface CustomAlertView()
- (void)alertDidFadeOut;
@end
@implementation CustomAlertView
@synthesize alertView;
@synthesize backgroundView;
@synthesize inputField;
@synthesize delegate;
#pragma mark -
#pragma mark IBActions
- (IBAction)show
{
// Retaining self is odd, but we do it to make this "fire and forget"
[self retain];
// We need to add it to the window, which we can get from the delegate
id appDelegate = [[UIApplication sharedApplication] delegate];
UIWindow *window = [appDelegate window];
[window addSubview:self.view];
// Make sure the alert covers the whole window
self.view.frame = window.frame;
self.view.center = window.center;
// "Pop in" animation for alert
[alertView doPopInAnimationWithDelegate:self];
// "Fade in" animation for background
[backgroundView doFadeInAnimation];
}
- (IBAction)dismiss:(id)sender
{
[inputField resignFirstResponder];
[UIView beginAnimations:nil context:nil];
// The 2 following lines replace the performSelector call below
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(alertDidFadeOut)];
self.view.alpha = 0.0;
[UIView commitAnimations];
// I commented this line and replaced it by the setAnimationDelegate and setAnimationDidStopSelector methods above
//[self performSelector:@selector(alertDidFadeOut) withObject:nil afterDelay:0.5];
if (sender == self || [sender tag] == CustomAlertViewButtonTagOk)
[delegate CustomAlertView:self wasDismissedWithValue:inputField.text];
else
{
if ([delegate respondsToSelector:@selector(customAlertViewWasCancelled:)])
[delegate customAlertViewWasCancelled:self];
}
}
#pragma mark -
- (void)viewDidUnload
{
[super viewDidUnload];
self.alertView = nil;
self.backgroundView = nil;
self.inputField = nil;
}
- (void)dealloc
{
[alertView release];
[backgroundView release];
[inputField release];
[super dealloc];
}
#pragma mark -
#pragma mark Private Methods
- (void)alertDidFadeOut
{
[self.view removeFromSuperview];
[self autorelease];
}
#pragma mark -
#pragma mark CAAnimation Delegate Methods
- (void)animationDidStop:(CAAnimation *)theAnimation finished:(BOOL)flag
{
[self.inputField becomeFirstResponder];
}
#pragma mark -
#pragma mark Text Field Delegate Methods
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self dismiss:self];
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment