2015-06-12 46 views
0

我無法對我的代碼中的錯誤進行排序。當我點擊「Submit」按鈕時,鍵盤再次上升。在這裏,我使用根據鍵盤高度滾動視圖的NSNotificatioCenter開啓按鈕動作NSNotificationCenter再次啓動鍵盤

看看下面的代碼。

-(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; 
    } 

} 

回答

1

添加的UITextField

的代表
@interface MyViewController : UIViewController <UITextFieldDelegate> 

現在你們textField.delegate = self;viewDidLoad

添加此委託方法來關閉鍵盤

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

OR

您可以將[self.view endEditing:YES];置於您的「提交」按鈕操作中以關閉鍵盤。

+0

[self.view endEditing:YES]; 謝謝你的回覆。 它適用於我,但它仍然可以在解僱後滾動視圖 –

+0

然後您可以重置提交按鈕操作中的滾動視圖。 –

+0

我猜你的'KeyboardShow'通知在提交按鈕後觸發。可能是它沒有妥善解決。 –