0

这是我的代码:

-(void)setViewMovedUp:(BOOL)movedUp
{
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.2]; // if you want to slide up the view
    [UIView setAnimationBeginsFromCurrentState:YES];

    CGRect rect = scroll.frame;
    if (movedUp)
    {
        rect.size.height += kOFFSET_FOR_KEYBOARD;
        rect.origin.y -= kOFFSET_FOR_KEYBOARD;
    }
    else
    {
        rect.size.height -= kOFFSET_FOR_KEYBOARD;
        rect.origin.y += kOFFSET_FOR_KEYBOARD;


    }
    scroll.frame = rect; 
    [UIView commitAnimations];
}


- (void)keyboardWillHide:(NSNotification *)notif {
    [self setViewMovedUp:NO];
}


- (void)keyboardWillShow:(NSNotification *)notif{
    [self setViewMovedUp:YES];
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification object:self.view.window];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) 
                                                 name:UIKeyboardWillHideNotification object:self.view.window];
}

- (void)viewWillDisappear:(BOOL)animated
{
    // unregister for keyboard notifications while not visible.
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

问题是,我怎样才能将它指定为仅适用于 TextView3,而不适用于 TextView2 或 TextView1。背景是,TextView1 和 TextView2 不需要,因为在我的视图之上。只有 TextView3 是不可读的 TextView,因为键盘被覆盖并且必须更正视图才能阅读我写的内容。

4

1 回答 1

2

为 textView3 分配一个特殊的标签,实现 TextView 委托方法——(void)textViewDidBeginEditing:(UITextView *)textView;在这里,您将检查 textView 标记是否等于 textView 3 标记调用您的函数-(void)setViewMovedUp:(BOOL)movedUp

更多代码:

好的,您需要像这样将 TextViewDelegate 导入到您的 viewController 中:在 .h viewController 文件中->viewController : UIViewController<UITextVIewDelegate>

然后在 viewDidload for ex 中分配这样的标签:textView3.tag = 222;

然后采用我在“textViewDidBeginEdit”上方编写的委托方法,并在内部创建一个 if 语句来检查 textView 标记是否等于 222 调用您的函数。

这是我现在能描述的最好的,因为我是用 iPhone 写的……祝你好运。

于 2012-04-11T15:38:23.993 回答