views:

112

answers:

1

Hi,

This seems to only occur with the new iOS 4 SDK. When I add a UITextField inside an UIAlertView, whenever I input any text inside the resulting popup, no text will show inside the TextField, even though I will be able to get the inputted text from the textField afterwards. This behavior does not happen when compiling against the old iPhone 3.1.x SDK instead of the newest iOS 4SDK.

Has anyone come across this problem as well?

Edit: posted source code. There's a text filter that filters out any non-numeric characters.

- (id)initWithFrame:(CGRect)frame textTag:(NSInteger)texttag title:(NSString *)intitle 

message:(NSString *)inmessage placeholder:inPlaceholder {
    if (self = [super initWithFrame:frame]) {
        textInput = [[UITextField alloc] initWithFrame:CGRectZero];
        textInput.borderStyle = UITextBorderStyleRoundedRect;
        textInput.keyboardType = UIKeyboardTypeNumbersAndPunctuation;
        textInput.returnKeyType = UIReturnKeyDone;
        textInput.placeholder = inPlaceholder;
        textInput.autocorrectionType = UITextAutocorrectionTypeNo;
        textInput.autocapitalizationType = UITextAutocapitalizationTypeNone;
        textInput.clearButtonMode = UITextFieldViewModeAlways;
        textInput.delegate = self;
        textInput.tag = texttag;

        self.title = intitle; //NSLocalizedString(@"Enter Price",@"Enter Price");
        self.message = inmessage; //NSLocalizedString(@"Input a custom price, then touch to confirm",@"Custom Price Prompt");

        [self addSubview:textInput];

        [self addButtonWithTitle:NSLocalizedString(@"Enter",@"Enter")];
        [self addButtonWithTitle:NSLocalizedString(@"Cancel",@"Cancel")];
        self.cancelButtonIndex = 1;

        allowedInputType = kText;

        [textInput becomeFirstResponder];
    }
    return self;
}

- (void)setFrame:(CGRect)rect {         
    [super setFrame:CGRectMake(0, 0, rect.size.width, 180)];         
    self.center = CGPointMake(320/2, 110); 
}

- (void)layoutSubviews {         
    CGFloat buttonTop = 0.0;         
    for (UIView *view in self.subviews) {                 
        if ([[[view class] description] isEqualToString:@"UIThreePartButton"]) {                         
            view.frame = CGRectMake(view.frame.origin.x, self.bounds.size.height - view.frame.size.height - 15, view.frame.size.width, view.frame.size.height);                         
            buttonTop = view.frame.origin.y;                 
        }         
    }         

    buttonTop -= 40;
    textInput.frame = CGRectMake(12, buttonTop, self.frame.size.width - 52, 30);
    //[textInput becomeFirstResponder];

}

- (void)drawRect:(CGRect)rect {         
    [super drawRect:rect];
}

-(void) dealloc {
    [textInput release];
    [super dealloc];
}

#pragma mark -
#pragma mark Event Handling

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];

    return YES;
}



- (void)textFieldDidBeginEditing:(UITextField *)textField {

    //[self addDecimalButtonToKeypad];

}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    NSCharacterSet *numberSet = nil;

    if (allowedInputType == kDecimalNumeric) {
        numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    } else if (allowedInputType == kIntegerNumeric) {
        numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    }

    if (numberSet != nil) {

        NSString *trimmed = [string stringByTrimmingCharactersInSet:numberSet];

        if (string.length > 0) {
            if ([trimmed length] > 0) {
                //Non-numeric character detected
                return NO;
            } else {

                NSScanner *scanner = [NSScanner scannerWithString:textField.text];

                NSInteger numberOfDots = 0;

                while ([scanner isAtEnd] == NO)
                {
                    if ([scanner scanUpToString:@"." intoString:NULL]) {
                        numberOfDots++;
                    } else {
                        break;
                    }

                }

                if (numberOfDots <= 1) {
                    return YES;
                } else {
                    return NO;
                }
            }
        } else {
            return YES;
        }
    } else {
        return YES;
    }
}

- (void)doneButton:(id)sender {

    NSLog(@"donebutton");
    [self findAndResignFirstResponder];
}

- (NSString *) getPrice {
    return textInput.text;
}