0

The issue is that my UITextField wrapper class isn't getting the UITextField to appear within a UITableViewCell (however, a regular UITextField view works just fine):

enter image description here

Here is my custom UITextField:

Header:

#import <UIKit/UIKit.h>

@protocol TUTextFieldDelegate <NSObject>

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

- (BOOL)textFieldShouldClear:(UITextField *)textField;

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

- (void)textFieldDidEndEditing:(UITextField *)textField;

@end

@interface TUTextField : UIControl <UITextFieldDelegate>

@property (nonatomic, assign) BOOL fixedDecimalPoint;
@property (nonatomic, assign) id <TUTextFieldDelegate>delegate;

@property (nonatomic, assign) UIKeyboardType keyboardType;
@property (nonatomic, strong) NSString *placeholder;
@property (nonatomic, assign) UITextBorderStyle borderStyle;
@property (nonatomic, assign) BOOL adjustsFontSizeToFitWidth;
@property (nonatomic, assign) UITextFieldViewMode clearButtonMode;
@property (nonatomic, strong) NSString *text;

- (id)initWithTextField:(UITextField *)textField;
- (id)initWithFrame:(CGRect)frame;

@end

Implementation:

#import "TUTextField.h"

@interface TUTextField ()

@property (nonatomic, strong) UITextField *textField;

@end

@implementation TUTextField

- (id)initWithTextField:(UITextField *)textField
{
    self = [super initWithFrame:textField.frame];
    if (self) {
        self.textField = textField;
        _textField.delegate = self;

        _fixedDecimalPoint = NO;
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.textField = [[UITextField alloc] initWithFrame:frame];
        _textField.delegate = self;
        [self addSubview:_textField];

        _fixedDecimalPoint = NO;
    }
    return self;
}

- (void)setKeyboardType:(UIKeyboardType)keyboardType {
    _textField.keyboardType = keyboardType;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _textField.placeholder = placeholder;
}

- (void)setBorderStyle:(UITextBorderStyle)borderStyle {
    _textField.borderStyle = borderStyle;
}

-(void)setAdjustsFontSizeToFitWidth:(BOOL)adjustsFontSizeToFitWidth {
    _textField.adjustsFontSizeToFitWidth = adjustsFontSizeToFitWidth;
}

-(void)setClearButtonMode:(UITextFieldViewMode)clearButtonMode {
    _textField.clearButtonMode = clearButtonMode;
}

-(void)setText:(NSString *)text {
    _textField.text = text;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    [self addSubview:self.textField];
}
*/

- (void)viewDidLoad {
    [self addSubview:_textField];
}

- (void)viewWillAppear {
    [self addSubview:_textField];
}


- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
replacementString:(NSString *)string
{
    if ([_delegate respondsToSelector:@selector(textField:shouldChangeCharactersInRange:replacementString:)]) {
        return [_delegate textField:textField shouldChangeCharactersInRange:range
                  replacementString:string];
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    if ([_delegate respondsToSelector:@selector(textFieldShouldClear:)]) {
        return [_delegate textFieldShouldClear:textField];
    }
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if ([_delegate respondsToSelector:@selector(textFieldDidBeginEditing:)]) {
        [_delegate textFieldDidBeginEditing:textField];
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if ([_delegate respondsToSelector:@selector(textFieldDidEndEditing:)]) {
        [_delegate textFieldDidEndEditing:textField];
    }
}

@end

And here is the code to add my custom wrapper to the table view:

    } else if (indexPath.row == 1) {
            cell.textLabel.text = @"Tax Rate";

            if (!_textField) {
                UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(220.0, 8.0, 60.0, 26.0)];
                textField.clearButtonMode = UITextFieldViewModeWhileEditing;
                textField.adjustsFontSizeToFitWidth = YES;
                textField.borderStyle = UITextBorderStyleRoundedRect;
                textField.keyboardType = UIKeyboardTypeDecimalPad;
                textField.placeholder = kTaxRateDefault;

                _textField = [[TUTextField alloc] initWithTextField:textField];
                _textField.delegate = self;

                // retrieve saved settings
                _taxRate = [[SettingsSingleton sharedManager] taxRate];
                if (![_taxRate isEqualToString:@""])
                    _textField.text = _taxRate;
            }

            UILabel *percentSign = [[UILabel alloc] initWithFrame:CGRectMake(285.0, 6.0, 20.0, 26.0)];
            percentSign.text = @"%";
            percentSign.backgroundColor = [UIColor clearColor];

            [cell addSubview:_textField];
            [cell addSubview:percentSign];
        }
    }

Any help appreciated!

Stunner
  • 12,025
  • 12
  • 86
  • 145

1 Answers1

0

I don't think this is the best way to extend a UITextField.

Instead of making an NSObject with a UITextField under it you should be saying something like:

Interface:

@interface UITextField (extendedUITextField) {

    // Variables

}

//Methods

@end

Implementation:

@implementation UITextField (extendedUITextField) 

     // Method implement

@end

Hope this helps.

Necro
  • 333
  • 5
  • 18
  • Actually I cannot do it this way as I need to be able to intercept delegate methods and a UITextField cannot be a delegate of itself: http://stackoverflow.com/q/1747777/347339 – Stunner Apr 16 '13 at 11:39