在按钮动作 NSNotificationCenter 再次升起键盘

On button action NSNotificationCenter Rises again the keyboard

我无法确定我的代码中的错误在哪里。当我单击 "Submit" 按钮时,键盘再次升起。在这里,我使用 NSNotificatioCenter 根据键盘高度滚动视图。

查看下面的代码。

-(void)viewDidLoad 
{
    [super viewDidLoad];
    [[self navigationController] setNavigationBarHidden:YES];

    // Register for the events
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil];

    // Setup content size
    _scrollView.contentSize = CGSizeMake(SCROLLVIEW_CONTENT_WIDTH,SCROLLVIEW_CONTENT_HEIGHT);

    keyboardVisible = NO;
}


-(void) viewWillDisappear:(BOOL)animated 
{
    NSLog (@"Unregister for keyboard events");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void) keyboardDidShow: (NSNotification *)notif 
{
    NSLog(@"Keyboard is visible");
    // If keyboard is visible, return
    if (keyboardVisible) {
        NSLog(@"Keyboard is already visible. Ignore notification.");
        return;
    }

    // Get the size of the keyboard.
    NSDictionary* info = [notif userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;

    // Save the current location so we can restore
    // when keyboard is dismissed
    offset = _scrollView.contentOffset;

    // Resize the scroll view to make room for the keyboard
    CGRect viewFrame = _scrollView.frame;
    viewFrame.size.height -= keyboardSize.height;
    _scrollView.frame = viewFrame;

    CGRect textFieldRect = [activeView frame];
    textFieldRect.origin.y += 10;
    [_scrollView scrollRectToVisible:textFieldRect animated:YES];

    NSLog(@"ao fim");
    // Keyboard is now visible
    keyboardVisible = YES;
}

-(void) keyboardDidHide: (NSNotification *)notif {
    // Is the keyboard already shown
    if (!keyboardVisible) {
        NSLog(@"Keyboard is already hidden. Ignore notification.");
        return;
    }

    // Reset the frame scroll view to its original value
    _scrollView.frame = CGRectMake(0, 0, SCROLLVIEW_CONTENT_WIDTH, SCROLLVIEW_CONTENT_HEIGHT);

    // Reset the scrollview to previous location
    _scrollView.contentOffset = offset;

    // Keyboard is no longer visible
    keyboardVisible = NO;

}

-(BOOL) textViewShouldBeginEditing:(UITextView*)textView {
    activeView = textView;
    return YES;
}

-(void)textViewDidEndEditing:(UITextView *)textView{
    if(textView == _textViewFeedback)
        [_textViewFeedback resignFirstResponder];
    else
        [_textViewEmail resignFirstResponder];
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
{
    if([text isEqualToString:@"\n"]){
        [textView resignFirstResponder];
    }
    return YES;
}


- (IBAction)BtnActionSubmitFeedback:(id)sender {
    _textViewEmail.text = nil;
    _textViewFeedback.text = nil;
    NSString *errorMessage = [self validateForm];
    if (errorMessage) {
        showAlert(@"Warning", errorMessage, nil, nil, @"Dismiss");
        return;
    }

}

添加 UITextField 委托

@interface MyViewController : UIViewController <UITextFieldDelegate>

现在将您的 textField.delegate = self; 设置为 viewDidLoad

添加此委托方法以关闭键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

您可以将 [self.view endEditing:YES]; 放入“提交”按钮操作中以关闭键盘。