Skip to content

Instantly share code, notes, and snippets.

@maddievision
Created March 9, 2011 09:56
Show Gist options
  • Save maddievision/861975 to your computer and use it in GitHub Desktop.
Save maddievision/861975 to your computer and use it in GitHub Desktop.
UI Alert View with Prompt
// taken from
// http://iphonedevelopment.blogspot.com/2009/02/alert-view-with-prompt.html
/*
Alert View with Prompt
Here's another generic class for you. This one doesn't require a navigation app - it can be used pretty much anywhere. It's a custom-subclass of UIAlertView that lets the user type in a value. It supports only two buttons - Okay and Cancel - but it handles everything to do with the text field for you. It looks like this:
*/
//
// AlertPrompt.h
// Prompt
//
// Created by Jeff LaMarche on 2/26/09.
#import <Foundation/Foundation.h>
@interface AlertPrompt : UIAlertView
{
UITextField *textField;
}
@property (nonatomic, retain) UITextField *textField;
@property (readonly) NSString *enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;
@end
// AlertPrompt.m
// Prompt
//
// Created by Jeff LaMarche on 2/26/09.
#import "AlertPrompt.h"
@implementation AlertPrompt
@synthesize textField;
@synthesize enteredText;
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{
if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
{
UITextField *theTextField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)];
[theTextField setBackgroundColor:[UIColor whiteColor]];
[self addSubview:theTextField];
self.textField = theTextField;
[theTextField release];
CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 130.0);
[self setTransform:translate];
}
return self;
}
- (void)show
{
[textField becomeFirstResponder];
[super show];
}
- (NSString *)enteredText
{
return textField.text;
}
- (void)dealloc
{
[textField release];
[super dealloc];
}
@end
#pragma mark - forks
/*
Good work. But these lines..
*/
theTextField.backgroundColor = [UIColor clearColor];
theTextField.borderStyle = UITextBorderStyleRoundedRect;
/*
.. make this look a lot nicer.
NOVEMBER 4, 2010 8:04 PM
*/
/*
mklement said...
Apologies - the previous code snippet only works with major iOS version numbers < 10; here's a version for the ages...
*/
NSString *iOsVersion = [[UIDevice currentDevice] systemVersion];
if ([iOsVersion intValue] < 4) {
[self setTransform:CGAffineTransformMakeTranslation(0,130)];
}
/*
NOVEMBER 12, 2010 4:12 PM
*/
/*
Crazy Data said...
"message" is not showed when "message" is short.
Add and Fix code in initWithTitle.
*/
NSString *msg = [ NSString stringWithFormat:@"\n%@", message ];
if (self = [super initWithTitle:title message:msg delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])
/*
u will show "message"!
MARCH 1, 2011 5:53 PM
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment