2013-07-10 12 views
5

私のアプリでは、たくさんのテキストフィールドを使用する必要があります。実際には、すべてのviewcontrollerクラスには面倒なテキストフィールドの代理人が含まれています。テキストフィールドの代理人の世話をするクラスで、テキストフィールドを返します。そこでは、必要な場所でサブビューとして追加できます。私はカスタムUItextFieldクラスを書くには

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]; 

がこれを可能にする。例えばテキストフィールド を必要とする時はいつでも、私は??クラスをライブラリとしてそれを作ると呼びたいです。 ありがとうございます!

答えて

7

を。この

.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 
+0

ありがとう@Parser self.delegate = selfを使うべきかどうかは確かではありませんでした。本当に助けてくれてありがとう。 – Vijay

1

UITextFieldのサブクラスを作成して使用します。

@interface CustomTexTField : UITextField 
@end 

@implementation CustomTexTField 

//Add the stuffs here 

@end 

あなたが使用できるテキスト・フィールド必要な場所:MidhunはカスタムTextFieldクラスを作成する必要があり、また、そのクラスにデリゲートを設定する回答として

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

私はすでに上記を試してみましたが、しかし、どのように私はデリゲートメソッドにアクセスすることができ、私は彼らが上になりたくありませんView Controllerクラスでは、CustomTextField?にそれらを含めることは可能でしょうか。 @Midhun – Vijay

0

のようにこれは私に

@interface CustomTextField : UITextField <UITextFieldDelegate> 

- (id)initWithFrame:(CGRect)frame { 
    if (self = [super initWithFrame:frame]) { 
     self.delegate = self; 
    } 
return self; 
} 
を助けました

は、delegateをcustomTextFieldクラスに追加してくれました。

ありがとうございます!

0

あなたはブロックを使用して、より良い方法でこれを達成することができます

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) 
    } 
} 

}

関連する問題