我需要添加邏輯來獲取靜態表格視圖單元部分索引的幫助。上一個/下一個UIToolbar在多個分段tableview故事板中使用多個文本字段
我有2個部分的純靜態的UITableView,在每個部分中有2個細胞與文本框。
我實現了一個代碼,添加工具欄與上一頁/下一頁/完成butoons鍵盤。
的上/下一個工作,如果該文本框是在第0,但下次再去下一個文本框這是在第1節,該應用程序崩潰時敲擊。
我應該在哪裏放置「if條件」來檢查文本框所在的部分?
這裏是我的.h文件中
#import <UIKit/UIKit.h>
@interface TextFieldTVCViewController : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate>
@property(strong, nonatomic) IBOutlet UITextField *dep1TextField;
@property(strong, nonatomic) IBOutlet UITextField *arr1TextField;
@property(strong, nonatomic) IBOutlet UITextField *dep2TextField;
@property(strong, nonatomic) IBOutlet UITextField *arr2TextField;
@property(strong, nonatomic) IBOutlet UIToolbar *keyboardToolbar;
@property(strong, nonatomic) IBOutlet UIBarButtonItem *toolbarActionButton;
@property(strong, nonatomic) NSArray *textFields;
- (IBAction) closeKeyboard;
- (IBAction) nextField;
- (IBAction) prevField;
@end
這裏是我的.m文件
#import "TextFieldTVCViewController.h"
@interface TextFieldTVCViewController()
@end
@implementation TextFieldTVCViewController
@synthesize dep1TextField, dep2TextField, arr1TextField, arr2TextField;
@synthesize keyboardToolbar, toolbarActionButton;
@synthesize textFields;
- (void)viewDidLoad
{
[super viewDidLoad];
textFields = [[NSArray alloc] initWithObjects:dep1TextField, arr1TextField, dep2TextField, arr2TextField, nil];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField{
[textField setInputAccessoryView:keyboardToolbar];
for (int i=0; i<[textFields count]; i++) {
if ([textFields objectAtIndex:i]==textField) {
if (i==[textFields count]-1) {
toolbarActionButton.title [email protected]"Done";
[toolbarActionButton setStyle:UIBarButtonItemStyleDone];
}
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}
}
}
- (IBAction) closeKeyboard{
for(UITextField *t in textFields){
if ([t isEditing]) {
[t resignFirstResponder];
break;
}
}
}
- (IBAction) nextField{
for (int i=0; i<[textFields count]; i++) {
if ([[textFields objectAtIndex:i] isEditing] && i!=[textFields count]-1) {
[[textFields objectAtIndex:i+1] becomeFirstResponder];
if (i+1==[textFields count]-1) {
[toolbarActionButton setTitle:@"Done"];
[toolbarActionButton setStyle:UIBarButtonItemStyleDone];
}else {
[toolbarActionButton setTitle:@"Close"];
[toolbarActionButton setStyle:UIBarButtonItemStyleBordered];
}
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i+1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
break;
}
}
}
- (IBAction) prevField{
for (int i=0; i<[textFields count]; i++) {
if ([[textFields objectAtIndex:i] isEditing] && i!=0) {
[[textFields objectAtIndex:i-1] becomeFirstResponder];
[toolbarActionButton setTitle:@"Close"];
[self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:i-1 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
[toolbarActionButton setStyle:UIBarButtonItemStyleBordered];
break;
}
}
}
@end
我把代碼從工具欄的網站:
http://idebuggerman.blogspot.com/2010/08/uitoolbar-for-keyboard-with.html?showComment=1371639814883
謝謝
您的回覆芒迪表示感謝。我會盡快回復你:) – kupilot