我有一個故事板,裏面插入了一個滾動條,因爲要顯示的內容太多而不能一次出現。 滾動條包含:當鍵盤出現時,UIScrollbar不會向上滾動
- 的圖像視圖
- 標籤
- 分組的表視圖
表視圖中的行兩種。第一種(由控制器regularRegistrationCellVC定義)包含一個文本字段和一個標籤。 當我點擊任何生成的單元格中的任何文本字段時,鍵盤出現並且textfield變爲隱藏。此外,不可能向上滾動視圖的內容以查看鍵盤下的內容。 所以,以下蘋果的指示,添加此代碼到包含滾動條,所述控制器:
註冊爲被叫鍵盤通知
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
當UIKeyboardDidShowNotification發送
- (void)keyboardWasShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
scrollPanel.contentInset = contentInsets;
scrollPanel.scrollIndicatorInsets = contentInsets;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= kbSize.height;
if (!CGRectContainsPoint(aRect, activeField.frame.origin)) {
CGPoint scrollPoint = CGPointMake(0.0, activeField.frame.origin.y-kbSize.height);
[scrollPanel setContentOffset:scrollPoint animated:YES];
}
}
鍵盤消失時調用
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
UIEdgeInsets contentInsets = UIEdgeInsetsZero;
scrollPanel.contentInset = contentInsets;
scrollPanel.scrollIndicatorInsets = contentInsets;
}
管理編輯開始的事件
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
activeField = textField;
NSLog(@"inizio digitazione\n");
}
結束編輯
- (void)textFieldDidEndEditing:(UITextField *)textField
{
activeField = nil;
}
其中activeField是我的控制器的一個全局變量的事件管理。
然後,我修改的代碼中創建的第一類單元的一部分,以這樣一種方式,第一種的每一個新的細胞有我的視圖控制器爲委託:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0) {
static NSString *CellIdentifier = @"regularRegistrationCell";
regularRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[regularRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.regularFieldName.text = [self.orientationItems objectAtIndex:indexPath.row];
cell.regularTextField.delegate = self;
return cell;
}
else{
static NSString *CellIdentifier = @"orientationRegistrationCell";
orientationRegistrationCellVC *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[orientationRegistrationCellVC alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.fieldLabel.text = [self.orientationItems objectAtIndex:[orientationItems count]-1];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
NSString *val = nil;
if (standardUserDefaults)
val = [standardUserDefaults objectForKey:@"orientation"];
if (val == nil)
cell.orientationLabel.text = @"Eterosessuale";
else
cell.orientationLabel.text = val;
return cell;
}
}
最後,我宣佈,我視圖控制器(包括滾動條及其內容)實現 UITextFieldDelegate。 使用NSLog打印,我發現每次單擊文本字段時,管理事件的函數肯定會執行。
你能告訴我錯在哪?
感謝您的幫助