有时候需要作个如下图的的输入密码页面, 每个框中只输入一个字母, 如果你只用了UITextFieldDelegate来作是不是非常难?
其实UITextField也是UIControl的子类 , 并且UIControlEvents中明确的提供了几个专门针对UITextField的枚举值
{
UIControlEventEditingDidBegin
UIControlEventEditingChanged
UIControlEventEditingDidEnd
UIControlEventEditingDidEndOnExit
UIControlEventAllEditingEvents
}
//
// PasswordInputMainView.h
//
//
// Created by willonboy zhang on 12-4-26.
// Copyright (c) 2012年 willonboy.tk. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Constant.h"
@protocol PasswordInputMainView <NSObject>
- (void)inputPwdComplete:(NSString *) pwd sender:(id)_sender;
@end
@interface PasswordInputMainView : UIView<UITextFieldDelegate>
{
int _currentTxtFieldIndex;
NSMutableString *_passwordStr;
UIImageView *_topNavImgView;
UIButton *_backBtn;
UILabel *_titleLabel;
}
@property(nonatomic, readonly)UITextField *passFirstTxtField;
@property(nonatomic, readonly)UITextField *passSecondTxtField;
@property(nonatomic, readonly)UITextField *passThirdTxtField;
@property(nonatomic, readonly)UITextField *passForthTxtField;
@property(nonatomic, readonly)UILabel *tipsMsgLabel;
@property(nonatomic, retain) NSString *tipsMsg;
@property(nonatomic, assign) id<PasswordInputMainView> delegate;
@end
//
// PasswordInputMainView.m
//
//
// Created by willonboy zhang on 12-4-26.
// Copyright (c) 2012年 willonboy.tk. All rights reserved.
//
#import "PasswordInputMainView.h"
@interface PasswordInputMainView()
- (void)initCustomView;
@end
@implementation PasswordInputMainView
@synthesize passFirstTxtField = _passFirstTxtField ;
@synthesize passSecondTxtField = _passSecondTxtField;
@synthesize passThirdTxtField = _passThirdTxtField ;
@synthesize passForthTxtField = _passForthTxtField ;
@synthesize tipsMsgLabel = _tipsMsgLabel;
@synthesize tipsMsg = _tipsMsg;
@synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
_currentTxtFieldIndex = 0;
_passwordStr = [[NSMutableString alloc] init];
}
return self;
}
- (void)dealloc
{
[_passFirstTxtField release];
[_passSecondTxtField release];
[_passThirdTxtField release];
[_passForthTxtField release];
[_tipsMsgLabel release];
[super dealloc];
}
- (void)layoutSubviews
{
[super layoutSubviews];
[self initCustomView];
}
- (void)initCustomView
{
if (_topNavImgView == nil)
{
_topNavImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 45)];
_topNavImgView.image = [UIImage imageNamed:@"topnavimg.png"];
_topNavImgView.userInteractionEnabled = YES;
[self addSubview:_topNavImgView];
[_topNavImgView release];
}
if (_titleLabel == nil)
{
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 5, 320, 35)];
_titleLabel.text = @"设置密码";
_titleLabel.textColor = [UIColor whiteColor];
_titleLabel.textAlignment = UITextAlignmentCenter;
_titleLabel.font = [UIFont fontWithName:FONT_DFPS size:20.0f];
_titleLabel.backgroundColor = [UIColor clearColor];
[self addSubview:_titleLabel];
[_titleLabel release];
}
if (_backBtn == nil)
{
_backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_backBtn.frame = CGRectMake(0, 0, 40, 37);
[_backBtn setImage:[UIImage imageNamed:@"backbtnimg.png"] forState:UIControlStateNormal];
[_backBtn setImage:[UIImage imageNamed:@"backbtnimgh.png"] forState:UIControlStateHighlighted];
[_backBtn addTarget:self action:@selector(dismissModalView) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:_backBtn];
}
if (_tipsMsgLabel == nil)
{
_tipsMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, 60, 220, 20)];
_tipsMsgLabel.textAlignment = UITextAlignmentCenter;
_tipsMsgLabel.font = [UIFont fontWithName:FONT_DFPS size:17.0f];
_tipsMsgLabel.textColor = [UIColor blackColor];
[self addSubview: _tipsMsgLabel];
}
_tipsMsgLabel.text = _tipsMsg;
[_tipsMsgLabel setNeedsLayout];
if (_passFirstTxtField == nil)
{
_passFirstTxtField = [[UITextField alloc] initWithFrame:CGRectMake(24, 100, 50, 40)];
_passFirstTxtField.textAlignment = UITextAlignmentCenter;
_passFirstTxtField.borderStyle = UITextBorderStyleBezel;
_passFirstTxtField.keyboardType = UIKeyboardTypeNumberPad;
_passFirstTxtField.secureTextEntry = YES;
_passFirstTxtField.userInteractionEnabled = YES;
_passFirstTxtField.tag = 101;
_passFirstTxtField.delegate = self;
[_passFirstTxtField addTarget:self action:@selector(endInputPwd:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:_passFirstTxtField];
}
if (_passSecondTxtField == nil)
{
_passSecondTxtField = [[UITextField alloc] initWithFrame:CGRectMake(98, 100, 50, 40)];
_passSecondTxtField.textAlignment = UITextAlignmentCenter;
_passSecondTxtField.borderStyle = UITextBorderStyleBezel;
_passSecondTxtField.keyboardType = UIKeyboardTypeNumberPad;
_passSecondTxtField.secureTextEntry = YES;
_passSecondTxtField.userInteractionEnabled = NO;
_passSecondTxtField.tag = 102;
_passSecondTxtField.delegate = self;
[_passSecondTxtField addTarget:self action:@selector(endInputPwd:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:_passSecondTxtField];
}
if (_passThirdTxtField == nil)
{
_passThirdTxtField = [[UITextField alloc] initWithFrame:CGRectMake(172, 100, 50, 40)];
_passThirdTxtField.textAlignment = UITextAlignmentCenter;
_passThirdTxtField.borderStyle = UITextBorderStyleBezel;
_passThirdTxtField.keyboardType = UIKeyboardTypeNumberPad;
_passThirdTxtField.secureTextEntry = YES;
_passThirdTxtField.userInteractionEnabled = NO;
_passThirdTxtField.tag = 103;
_passThirdTxtField.delegate = self;
[_passThirdTxtField addTarget:self action:@selector(endInputPwd:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:_passThirdTxtField];
}
if (_passForthTxtField == nil)
{
_passForthTxtField = [[UITextField alloc] initWithFrame:CGRectMake(246, 100, 50, 40)];
_passForthTxtField.textAlignment = UITextAlignmentCenter;
_passForthTxtField.borderStyle = UITextBorderStyleBezel;
_passForthTxtField.keyboardType = UIKeyboardTypeNumberPad;
_passForthTxtField.secureTextEntry = YES;
_passForthTxtField.userInteractionEnabled = NO;
_passForthTxtField.tag = 104;
_passForthTxtField.delegate = self;
[_passForthTxtField addTarget:self action:@selector(endInputPwd:) forControlEvents:UIControlEventEditingChanged];
[self addSubview:_passForthTxtField];
}
[_passFirstTxtField becomeFirstResponder];
}
- (void)dismissModalView
{
[(UIViewController *)self.delegate dismissModalViewControllerAnimated:YES];
}
#pragma mark – UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
if (textField && textField.text && textField.text.length > 0)
{
[_passwordStr appendString:textField.text];
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(textField.text.length == 1)
{
return NO;
}
return YES;
}
- (void)endInputPwd:(id) sender
{
UITextField *txtField = (UITextField *)sender;
switch (txtField.tag)
{
case 101:
_passFirstTxtField.userInteractionEnabled = NO;
_passSecondTxtField.userInteractionEnabled = YES;
[_passSecondTxtField becomeFirstResponder];
break;
case 102:
_passSecondTxtField.userInteractionEnabled = NO;
_passThirdTxtField.userInteractionEnabled = YES;
[_passThirdTxtField becomeFirstResponder];
break;
case 103:
_passThirdTxtField.userInteractionEnabled = NO;
_passForthTxtField.userInteractionEnabled = YES;
[_passForthTxtField becomeFirstResponder];
break;
case 104:
_passForthTxtField.userInteractionEnabled = NO;
NSLog(@"输入完成 result is %@", _passwordStr);
if (self.delegate && [self.delegate respondsToSelector:@selector(inputPwdComplete: sender:)])
{
[self.delegate inputPwdComplete:[_passwordStr description] sender:self];
}
break;
default:
break;
}
}
@end