【问题标题】:How to write a Custom UItextField Class如何编写自定义 UItextField 类
【发布时间】:2017-05-10 17:05:38
【问题描述】:

在我的应用程序中,我需要使用很多文本字段,我真的不希望每个视图控制器类都包含可能很混乱的文本字段的代表,我只想创建一个通用类来处理文本字段的代表并返回给我一个文本字段,我可以在其中将其添加为我需要的子视图。我想把它变成一个库,并在需要文本字段时调用该类 例如

CustomTexTField *textField = [[CustomTextField alloc] initWithFrame:Frame];
// returns  a textField whose delegate will be set to CustomTextField //
// all i should do is just adding it as a subView //
[self.view addSubView:textField];

这可能吗? 提前致谢!!

【问题讨论】:

    标签: ios objective-c uitextfield


    【解决方案1】:

    正如 Midhun 回答的那样,您需要创建一个自定义 TextField 类并在该类中设置委托。像这样

    .h 文件

    @interface CustomTextField : UITextField<UITextFieldDelegate>
    @end
    

    .m 文件

    @implementation CustomTextField
    - (id)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            // Initialization code
            self.delegate = self;
        }
    return self;
    }
    - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
        return YES;
    }
    - (void)textFieldDidBeginEditing:(UITextField *)textField{
    }
    - (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
        return YES;
    }
    - (void)textFieldDidEndEditing:(UITextField *)textField{
    }
    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
        return YES;
    }
    - (BOOL)textFieldShouldClear:(UITextField *)textField{
        return YES;
    }
    - (BOOL)textFieldShouldReturn:(UITextField *)textField{
        return YES;
    }
    @end
    

    【讨论】:

    • 谢谢@Parser 我不确定我们是否应该使用 self.delegate = self;谢谢它真的帮助了我。
    【解决方案2】:

    创建UITextField 的子类并使用它。

    @interface CustomTexTField : UITextField
    @end
    
    @implementation CustomTexTField
    
    //Add the stuffs here
    
    @end
    

    您可以在任何需要文本字段的地方使用:

    CustomTexTField *textField = [[CustomTextField alloc] initWithFrame:customFrame];
    [self.view addSubView:textField];
    

    【讨论】:

    • 我已经尝试过上述方法但是我怎样才能访问委托方法,我不希望它们出现在视图控制器类中,是否可以将它们包含在 CustomTextField 中? @Midhun
    【解决方案3】:

    您可以通过使用块的更好方法来实现这一点:

    class MyTextField: UITextField, UITextFieldDelegate {
    
    
    //MARK:- PROPERTIES
    
    var shouldPreventAllActions:Bool = false
    
    var canCut:Bool = true
    var canCopy:Bool = true
    var canPaste:Bool = true
    var canSelect:Bool = true
    var canSelectAll:Bool = true
    
    var blockTextFieldShouldChangeCharactersInRangeWithReplacementString:((_ textField: UITextField, _ range: NSRange, _ string: String) -> Bool)?
    var blockTextFieldShouldReturn:((_ textField: UITextField) -> Bool)?
    var blockTextFieldShouldClear:((_ textField: UITextField) -> Bool)?
    //MARK:-
    var blockTextFieldShouldBeginEditing:((_ textField: UITextField) -> Bool)?
    var blockTextFieldShouldEndEditing:((_ textField: UITextField) -> Bool)?
    //MARK:-
    var blockTextFieldDidBeginEditing:((_ textField: UITextField) -> Void)?
    var blockTextFieldDidEndEditing:((_ textField: UITextField) -> Void)?
    var blockTextFieldDidEndEditingWithReason:((_ textField: UITextField, _ reason: UITextFieldDidEndEditingReason) -> Void)?
    
    
    //MARK:- INIT
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        commonInit()
    }
    
    private func commonInit(){
        // common initialization code..
    }
    
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
    
        if(self.shouldPreventAllActions){
            return false
        }
    
        switch action {
        case #selector(UIResponderStandardEditActions.cut(_:)):
            return self.canCut ? super.canPerformAction(action, withSender: sender) : self.canCut
        case #selector(UIResponderStandardEditActions.copy(_:)):
            return self.canCopy ? super.canPerformAction(action, withSender: sender) : self.canCopy
        case #selector(UIResponderStandardEditActions.paste(_:)):
            return self.canPaste ? super.canPerformAction(action, withSender: sender) : self.canPaste
        case #selector(UIResponderStandardEditActions.select(_:)):
            return self.canSelect ? super.canPerformAction(action, withSender: sender) : self.canSelect
        case #selector(UIResponderStandardEditActions.selectAll(_:)):
            return self.canSelectAll ? super.canPerformAction(action, withSender: sender) : self.canSelectAll
        default:
            return super.canPerformAction(action, withSender: sender)
        }
    }
    
    //MARK:- DELEGATE
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    
        if(self.blockTextFieldShouldChangeCharactersInRangeWithReplacementString != nil){
            return self.blockTextFieldShouldChangeCharactersInRangeWithReplacementString!(textField,range,string)
        }else{
            return true
        }
    }
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    
        if(self.blockTextFieldShouldReturn != nil){
            return self.blockTextFieldShouldReturn!(textField)
        }else{
            return true
        }
    }
    func textFieldShouldClear(_ textField: UITextField) -> Bool {
    
        if(self.blockTextFieldShouldClear != nil){
            return self.blockTextFieldShouldClear!(textField)
        }else{
            return true
        }
    }
    
    
    //MARK:-
    func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    
        if(self.blockTextFieldShouldBeginEditing != nil){
            return self.blockTextFieldShouldBeginEditing!(textField)
        }else{
            return true
        }
    }
    func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
    
        if(self.blockTextFieldShouldEndEditing != nil){
            return self.blockTextFieldShouldEndEditing!(textField)
        }else{
            return true
        }
    }
    
    //MARK:-
    func textFieldDidBeginEditing(_ textField: UITextField) {
    
        if(self.blockTextFieldDidBeginEditing != nil){
            self.blockTextFieldDidBeginEditing!(textField)
        }
    }
    func textFieldDidEndEditing(_ textField: UITextField) {
    
        if(self.blockTextFieldDidEndEditing != nil){
            self.blockTextFieldDidEndEditing!(textField)
        }
    }
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
    
        if(self.blockTextFieldDidEndEditingWithReason != nil){
            self.blockTextFieldDidEndEditingWithReason!(textField,reason)
        }
    }
    

    }

    【讨论】:

      【解决方案4】:

      这对我有帮助

      @interface CustomTextField : UITextField <UITextFieldDelegate> 
      
      - (id)initWithFrame:(CGRect)frame {
          if (self = [super initWithFrame:frame]) {
              self.delegate = self;
          }
      return self;
      }
      

      向 customTextField 类添加了委托,它对我有用。

      谢谢!!

      【讨论】:

        猜你喜欢
        • 2018-06-20
        • 2022-07-29
        • 2020-02-10
        • 1970-01-01
        • 2017-10-03
        • 2011-10-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多