2012-09-15 29 views
1

我有一個應用程序,其中我通過使用戶點擊編輯按鈕,其示出了在該表中,類似於內置在通知應用程序的底部的文本字段小區添加新項目表視圖。當顯示鍵盤時,我需要調整表格,以便在表格中有很多行時不會遮擋它。我通過訂閱通知時,鍵盤顯示這樣做:如何在多個視圖上處理keyboardDidShow?

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector (keyboardDidShow:) 
               name: UIKeyboardDidShowNotification 
               object:nil]; 

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

... 
... 

-(void) keyboardDidShow: (NSNotification *)notif 
{ 
    // If keyboard is visible, return 
    if (self.keyboardVisible) 
    { 
     return; 
    } 

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

    // Adjust the table view by the keyboards height. 
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, keyboardSize.height, 0); 
    NSIndexPath *path = [NSIndexPath indexPathForRow:self.newsFeeds.count inSection:0]; 
    [self.tableView scrollToRowAtIndexPath:path atScrollPosition:UITableViewScrollPositionTop animated:YES]; 
    self.keyboardVisible = YES; 
} 

不過,我讓用戶添加行的表,也可以被挖掘和一個新的觀點推到應用程序。這個視圖也有一個文本視圖,當用戶點擊它時,鍵盤顯示第一個視圖控制器仍然收到通知,導致崩潰。

如何,我不是可以忽略的通知或獲得當一個新視圖推它不火?

+0

你可以添加類在viewDidAppear觀察員和viewWillDisappear刪除它。 – InsertWittyName

+0

我試着用名字刪除較早,它拋出一個異常,但我不明白爲什麼... – lehn0058

+0

我想通了,爲什麼這是行不通的。我無意中稱[自我viewWillDisappear:動畫];而不是[超級viewWillDisappear:動畫];這使我陷入無限循環。使用viewWillDisappear可以完美地工作。 – lehn0058

回答

1

你可以添加類在viewDidAppear觀察員和viewWillDisappear刪除它。

相關問題