UITextField的UIControl用法

有时候需要作个如下图的的输入密码页面, 每个框中只输入一个字母, 如果你只用了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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值