2013-07-10 4 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

Midhun 당신이 그 클래스에서 사용자 지정 TextField 클래스도 설정 대리자를 만들 필요가 답변으로 :

@interface CustomTexTField : UITextField 
@end 

@implementation CustomTexTField 

//Add the stuffs here 

@end 

은 어디든지 당신은 당신이 사용할 수있는 텍스트 필드가 필요합니다. 이

.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

Thanks @Parser self.delegate = self를 사용해야하는지 확신 할 수 없었습니다. 정말 고마워. – Vijay

1

UITextField의 하위 클래스를 만들어 사용하십시오.

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

는 이미 위의 시도했지만 내가 어떻게이 대리자 메서드에 액세스 할 수 있습니다, 나는 그들이에 싶지 않아 보기 컨트롤러 클래스, 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) 
    } 
} 

가}

관련 문제