Skip to content

Instantly share code, notes, and snippets.

@RSully
Created June 17, 2011 16:02
Show Gist options
  • Save RSully/1031706 to your computer and use it in GitHub Desktop.
Save RSully/1031706 to your computer and use it in GitHub Desktop.
Easy to use UIAlertView subclass to get text input
Usage:
-(void)doStuff:(id)sender {
NSString *text = [RSAlertViewTextInput textWithTitle:@"Enter your zipcode" cancelBtn:@"Cancel" submitBtn:@"Submit"];
NSLog(@"nom: %@", text);
}
Easy to use, one line of code. Or you can initialize it and figure it out that way.
//
// RSAlertViewTextInput.h
// Forecast
//
// Created by Ryan Sullivan on 6/17/11.
// Copyright 2011 Freelance Web Developer. All rights reserved.
//
#import <Foundation/Foundation.h>
@class RSAlertViewTextInput;
@interface RSAlertViewTextInput : UIAlertView <UIAlertViewDelegate> {
UITextField *text;
BOOL isTehHiddenz;
}
+(NSString*)textWithTitle:(NSString*)t cancelBtn:(NSString*)c submitBtn:(NSString*)s;
-(BOOL)isTehHiddenz;
-(UITextField*)text;
-(void)setTitle:(NSString*)t message:(NSString*)m cancelButton:(NSString*)can submitButton:(NSString*)sub;
@end
//
// RSAlertViewTextInput.m
// Forecast
//
// Created by Ryan Sullivan on 6/17/11.
// Copyright 2011 Freelance Web Developer. All rights reserved.
//
#import "RSAlertViewTextInput.h"
@implementation RSAlertViewTextInput
+(NSString*)textWithTitle:(NSString*)t cancelBtn:(NSString*)c submitBtn:(NSString*)s {
RSAlertViewTextInput *test = [[RSAlertViewTextInput alloc] init];
[test setTitle:t message:nil cancelButton:c submitButton:s];
[test show];
do {
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1]];
//[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
} while (![test isTehHiddenz]);
[test autorelease];
return [[test text] text];
}
-(UITextField*)text { return text; }
-(BOOL)isTehHiddenz { return isTehHiddenz; }
-(id)init {
self = [super init];
if (self) {
[self setDelegate:self];
text = [[UITextField alloc] initWithFrame:CGRectMake(12, 45, 260, 25)];
[text setBackgroundColor:[UIColor whiteColor]];
[text setTextAlignment:UITextAlignmentCenter];
isTehHiddenz = NO;
}
return self;
}
-(void)dealloc {
[text release];
[super dealloc];
}
-(void)setTitle:(NSString*)t message:(NSString*)m cancelButton:(NSString*)can submitButton:(NSString*)sub {
[self setTitle:t];
[self setMessage:@"This is hidden"];
[self addButtonWithTitle:can];
[self setCancelButtonIndex:0];
[self addButtonWithTitle:sub];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == [self cancelButtonIndex]) {
// Do things?
}
isTehHiddenz = YES;
}
-(void)show {
[text removeFromSuperview];
[self addSubview:text];
[super show];
[text becomeFirstResponder];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment