【问题标题】:NSNotification problem for textfield and textviewtextfield 和 textview 的 NSNotification 问题
【发布时间】:2009-12-19 13:44:03
【问题描述】:

实际上我在表格视图的第六部分有文本字段,在文本视图的第十一部分有一个文本视图。我的问题是当我点击第六部分的文本字段时,键盘覆盖了文本字段,同样的事情是我使用 NSNotificationCenters 的 textview.for 发生了。 我把代码写成:

-(void)viewWillAppear:(BOOL)动画{

tableView.tableFooterView=nil;
[self reloadView];  
[[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];
[tableView reloadData]; 

}

-(void)setViewMovedUp:(BOOL)movedUp{

[UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.5]; // 如果你想向上滑动视图 CGRect 矩形 = mview.frame; 如果(上移) {
rect.origin.y = rect.origin.y - kOFFSET_FOR_KEYBOARD; rect.size.height = rect.size.height + kOFFSET_FOR_KEYBOARD; } 别的 { rect.origin.y = rect.origin.y + kOFFSET_FOR_KEYBOARD; rect.size.height = rect.size.height - kOFFSET_FOR_KEYBOARD; } mview.frame = rect;

[UIView commitAnimations];

}

  • (void)keyboardWillShow:(NSNotification *)notif { // printf("\n 键盘将显示"); //现在将显示键盘。根据哪个文本字段处于活动状态,适当地向上或向下移动视图

    if ([notesTextView isFirstResponder] && mview.frame.origin.y >= 0) {
    [自我 setViewMovedUp:YES]; } else if (![notesTextView isFirstResponder] && mview.frame.origin.y

    else if ([wineryTextField isFirstResponder] && mview.frame.origin.y >= 0) {
    [自我 setViewMovedUp:YES]; } else if (![wineryTextField isFirstResponder] && mview.frame.origin.y

}

  • (void)keyboardWillHide:(NSNotification *)notif { // printf("\n 键盘将隐藏"); //现在将显示键盘。根据哪个文本字段处于活动状态,适当地向上或向下移动视图

    if ([notesTextView isFirstResponder] && mview.frame.origin.y >= 0) { [自我 setViewMovedUp:YES]; } else if (![notesTextView isFirstResponder] && mview.frame.origin.y

    else if ([wineryTextField isFirstResponder] && mview.frame.origin.y >= 0) { [自我 setViewMovedUp:YES]; } else if (![wineryTextField isFirstResponder] && mview.frame.origin.y

}

  • (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range 替换文本:(NSString *)文本 { if ( [ text isEqualToString: @"\n" ] ) {

    //event.eventNotes = notesTextView.text;
    
     if(textView==notesTextView)
    {
    
        //[self setViewMovedUp:NO];
        [textView resignFirstResponder];
    }
    return NO;
    

    }

    返回是;

}

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

    if(textField == wineryTextField) { [自我 setViewMovedUp:NO]; [wineryTextField resignFirstResponder]; } }

以上是我为滚动视图编写的代码。但问题是有时当我单击文本字段时它会向下滚动而不是向上滚动。textView 也是如此。问题出现在何时我导航到另一个控制器,然后返回并单击八个 texfield 或 textview。

请任何人为我的问题提供完美的解决方案。

谢谢大家.....

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    您可能要考虑使用以下 UITableView 方法:

    - (void)selectRowAtIndexPath:(NSIndexPath *)indexPath
                        animated:(BOOL)animated
                  scrollPosition:(UITableViewScrollPosition)scrollPosition
    

    其中 indexPath 包含您希望在顶部显示的行,而 scrollPosition 是 UITableViewScrollPositionTop。

    如果您希望 tableview 向后滚动,请使用以下 UITableView 方法保存可见屏幕顶部的行。

    - (NSArray *)indexPathsForVisibleRows
    

    然后使用上面提到的第一种方法,滚动到保存的行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      相关资源
      最近更新 更多