2012-02-23 3 views
1

나는 다음과 같은 코드가 있습니다목표 - C 리팩토링 방법

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 
CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 
... 

} 

당신은 그것의 UITextField에 전달 볼 수 있듯이을. 나는 또한이 코드를 동일한 ViewController에 복제했지만 UITextView를 전달했습니다.

UITextField 또는 UITextView 중 하나를 전달하는 단일 메서드로 리팩터링 할 수 싶습니까? 어떻게해야합니까?

이 코드는 다른보기 컨트롤러에도 나타나므로 이상적으로 iOS의 새로운 도우미 클래스에 배치하고 싶습니다. 그래서 여기에서 어디로 가야할지 모르겠습니다.

간결함을 위해 대부분의 코드를 제거했지만, iOS 키보드가 나타나면 UI 컨트롤을보기로 슬라이드합니다.

답변

4

이러한 뷰의 특수 텍스트 속성을 사용하지 않는 것 같기 때문에 UIView를 사용할 수 있습니다.

-(void)textFieldDidBeginEditing:(UIView *)textField 
{ 
    CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; 
    CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; 
    // ... 
} 
3

일반적인 수퍼 클래스 인 UIView가 필요한 도우미 메서드를 호출합니다.

-(void)textFieldDidBeginEditing:(UITextField *)textField 
{ 
    return [self textBeginEditing:textField]; 
} 


-(void)textViewDidBeginEditing:(UITextView *)textView 
{ 
    return [self textBeginEditing:textView]; 
} 


-(void)textBeginEditing:(UIView *)view 
{ 
    //and if you need to do something, where you need to now, if it is a textView or a field, use 

    if([view isKindOfClass:[UITextField class]]){ 
     //… 
    } else if([view isKindOfClass:[UITextView class]]){ 
     //… 
    } 
}