Thursday, February 26, 2009

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:


You use it pretty much the same way you use an alert view. You allocate and initialize it, call show and then release it:

    AlertPrompt *prompt = [AlertPrompt alloc];
prompt = [prompt initWithTitle:@"Test Prompt" message:@"Please enter some text in" delegate:self cancelButtonTitle:@"Cancel" okButtonTitle:@"Okay"];
[prompt show];
[prompt release];

Then, you implement the appropriate UIAlertView callback method, and grab the entered text from the alert view instance. You have to cast the alert view back to an AlertPrompt instance, but other than that, everything is the same as using a standard UIAlertview:

- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
NSString *entered = [(AlertPrompt *)alertView enteredText];
label.text = [NSString stringWithFormat:@"You typed: %@", entered];
}

}


You can download a sample project that shows how it works right here.

Note: to those of you wondering if this violates the HIG, or will cause problems during review - I don't think it should. Apple does this themselves when they prompt you for a WiFi network password, and I have not used any private APIs or functions whatsoever. I just subclassed an existing public class and extended its functionality in a way that's commonly done.

The code for the class follows:

AlertPrompt.h
//
// 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
//
// 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

No comments:

Post a Comment