在代码中为UITextField分配方法

时间:2012-07-16 17:19:42

标签: objective-c uitextfield

我的视图底部有一个文本字段。当您单击以编辑它时,键盘将其覆盖,您无法看到您正在键入的内容。我想创建一个方法,在编辑该文本字段时重新调整视图。

如果我通过界面构建​​器声明了文本字段,那么我知道我所要做的就是链接(按住ctrl然后拖动)它。但我在代码中声明了文本字段,所以我想知道如何为它分配一个方法。

我想做的伪代码:

if (the text field is currently being edited){
    call method: adjust view
}

4 个答案:

答案 0 :(得分:3)

在.h中实现UITextFieldDelegate,如下所示:

@interface ShippingInfoViewController : UIViewController < UITextFieldDelegate>

并确保将textField的委托设置为self

然后您可以访问许多方法,例如

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

-(void) textFieldDidBeginEditing:(UITextField *)textField{

-(BOOL)textFieldShouldReturn:(UITextField *)textField{

- (void)textFieldDidEndEditing:(UITextField *)textField {

和其他许多人。

apple reference

答案 1 :(得分:1)

创建文本字段时,将其委托设置为self。

[textField setDelegate:self];

然后实现委托方法

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    //adjust view here
}

- (void)textFieldDidEndEditing:(UITextField *)textField {
    //move view back to correct position here
}

您还可以注册UIKeyboardWillShow / UIKeyboardWillHide或UIKeyboardDidShow / UIKeyboardDidHide消息,并为此实现功能。

答案 2 :(得分:0)

您必须为文本视图分配一个代理:

[textView setDelegate: self]

我假设,self是您的视图控制器,需要符合UITextViewDelegate protocol。每当文本视图开始编辑周期时,它都会调用textViewShouldBeginEditing: / textViewWillBeginEditing

答案 3 :(得分:0)

使用以下功能:

- (void)viewWillAppear:(BOOL)animated
{
   [super viewWillAppear:animated];

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}


- (void)keyboardWillShow:(NSNotification *)notification
{  
  [self moveTextViewForKeyboard:notification up:YES];
}


- (void)keyboardWillHide:(NSNotification *)notification
{
  [self moveTextViewForKeyboard:notification up:NO];
}


- (void)moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up {
   NSDictionary *userInfo = [notification userInfo];
   NSTimeInterval animationDuration;
   UIViewAnimationCurve animationCurve;
   CGRect keyboardRect;

  [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
  animationDuration = [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
  keyboardRect = [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  keyboardRect = [self.view convertRect:keyboardRect fromView:nil];

  [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
  [UIView setAnimationDuration:animationDuration];
  [UIView setAnimationCurve:animationCurve];

 if (up == YES) {
    CGFloat keyboardTop = keyboardRect.origin.y;
    CGRect newTextViewFrame = self.noteTextView.frame;
    originalTextViewFrame = self.noteTextView.frame;
    newTextViewFrame.size.height = keyboardTop - self.noteTextView.frame.origin.y - 10;

    self.noteTextView.frame = newTextViewFrame;
 } else {
    // Keyboard is going away (down) - restore original frame
    self.noteTextView.frame = originalTextViewFrame;
 }

  [UIView commitAnimations];
}

对originalTextViewFrame使用静态变量:

static CGRect originalTextViewFrame;