2011-10-29 37 views
5

我只花了一大部分每天跟蹤,其中呼籲積極的UITextField resignFirstResponder沒有隱藏鍵盤一個很奇怪的情況下,即使文本字段是第一個響應者。當我使用活動文本字段將視圖控制器推到另一個視圖控制器的頂部時,會發生這種情況。鍵盤消失(如預期)。但是,如果通過觸摸第二個視圖控制器中的文本字段來恢復鍵盤,則後續調用resignFirstResponder將不起作用。無法隱藏鍵盤的UIViewController堆棧時UIAlertView中在屏幕上

下面是簡單的代碼來重現問題。這段代碼是一個帶有導航欄按鈕的視圖控制器,用於隱藏鍵盤,另一個用於推送自身的另一個副本(帶有確認UIAlertView)。第一個副本沒有問題。但是,如果您按第二個副本(當第一個副本具有可見的鍵盤時),則不可能關閉鍵盤。這隻有在按下第二個副本時在屏幕上出現UIAlertView(確認)時纔會發生。如果你刪除#define ALERT行,一切正常。

有誰知道這裏發生了什麼?它看起來像UIALertView窗口以某種方式干擾了鍵盤,並保持它的窗口不再消失,然後混淆下一個視圖。除了在UIALertView不見了之後將第二個視圖控制器放在定時器上之外,有沒有其他解決方案?

對不起,複雜的描述。這是可運行的代碼。我希望代碼清楚。

@implementation DemoViewController 

- (id) init { 
    if (!(self = [super init])) 
     return nil; 

    return self; 
} 

- (void) dealloc { 
    [_inputTextfield release]; 
    [super dealloc]; 
} 

- (void) loadView { 
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds]; 

    _inputTextfield = [[UITextField alloc] initWithFrame:CGRectMake(0., 0., 320., 44.)]; 
    _inputTextfield.borderStyle = UITextBorderStyleRoundedRect; 
    _inputTextfield.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
    _inputTextfield.keyboardAppearance = UIKeyboardAppearanceAlert; 
    _inputTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone; 
    _inputTextfield.autocorrectionType = UITextAutocorrectionTypeNo; 
    _inputTextfield.keyboardType = UIKeyboardTypeDefault; 
    [view addSubview:_inputTextfield]; 

    self.view = view; 
    [view release]; 
} 

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

    UIButton *downButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [downButton setTitle: @"keyboard down" forState:UIControlStateNormal]; 
    [downButton addTarget:self action:@selector(downButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [downButton sizeToFit];  
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:downButton] autorelease]; 

    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    [nextButton setTitle: @"next" forState:UIControlStateNormal]; 
    [nextButton addTarget:self action:@selector(nextButtonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [nextButton sizeToFit]; 
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:nextButton] autorelease];; 

} 

- (void) viewWillDisappear:(BOOL) animated { 
[super viewWillDisappear:animated]; 
    [_inputTextfield resignFirstResponder]; 
} 

- (void) downButtonPressed:(id)sender { 
    [_inputTextfield resignFirstResponder]; 
} 

#define ALERT 

- (void) alertView:(UIAlertView *) alertView didDismissWithButtonIndex:(NSInteger) buttonIndex { 
    if (alertView.cancelButtonIndex == buttonIndex) { 
     return; 
    } 
    [self _nextButtonPressed]; 
} 

- (void) _nextButtonPressed { 
    DemoViewController *nextViewController = [[DemoViewController alloc] init];  
    [self.navigationController pushViewController:nextViewController]; 
    [nextViewController release]; 
} 

- (void) nextButtonPressed:(id)sender { 
#ifdef ALERT 
    UIAlertView *alert = [[UIAlertView alloc] init]; 
    alert.message = @"Next view?"; 
    alert.cancelButtonIndex = [alert addButtonWithTitle:@"No"]; 
    [alert addButtonWithTitle:@"Yes"]; 
    alert.delegate = self; 
    [alert show]; 
    [alert release]; 
#else 
    [self _nextButtonPressed];  
#endif 
} 
+0

你試過呈現第二視圖控制器之前辭職的第一個視圖控制器上的第一個響應者? –

+0

是的,在viewWillDisappear。對不起,這並沒有讓我的演示代碼。但它沒有效果。 – wombat57

+0

好吧,你有沒有嘗試過使用[self.view endEditing:YES]?這應該也會在所有子視圖上結束編輯。 –

回答

4

如果你運氣不好你辭職第一響應者,這裏有一些解決方案,可以幫助:

  1. 決定誰一直保持第一響應上次通話後辭職第一響應。

  2. 試圖通過一個單一的通話辭職所有的第一反應,以self.view(容器視圖)

    [self.view endEditing:YES]; 
    
  3. ONLY如果你已經嘗試了所有上述的方法,沒有工作,可以考慮使用此解決方法。

    -(BOOL)textViewShouldEndEditing:(UITextView *)textView { 
        NSArray *wins = [[UIApplication sharedApplication] windows]; 
        if ([wins count] > 1) { 
        UIWindow *keyboardWindow = [wins objectAtIndex:1]; 
        keyboardWindow.hidden = YES; 
        } 
        return YES; 
    }