objective-c重构方法
我有以下代码:objective-c重构方法,objective-c,ios,cocoa-touch,Objective C,Ios,Cocoa Touch,我有以下代码: -(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]; ... }
-(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的方法?我该怎么做
这段代码也出现在其他视图控制器中,所以理想情况下,我想将它放在一个helper类中,这对于iOS来说是非常新的,所以不确定从这里去哪里
为简洁起见,我已从方法中删除了大部分代码,但它所做的是在iOS键盘出现时将UI控件滑入视图。您可以使用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];
// ...
}
您可以使用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];
// ...
}
调用一个助手方法,该方法需要一个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]]){
//…
}
}
调用一个助手方法,该方法需要一个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]]){
//…
}
}