2013-06-19 23 views
0

我需要添加邏輯來獲取靜態表格視圖單元部分索引的幫助。上一個/下一個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

謝謝

回答

0

我認爲你的方法來保持一個文本字段數組不是很乾淨。它會導致非常複雜的代碼,你必須迭代很多,這很容易出錯。

相反,使用一個枚舉標籤文本字段。在「標籤」下的故事板中插入適當的數字。在你的實現文件之上,定義你的字段。

typedef enum { 
    Dep1Field = 100, Arr1Field, 
    Dep2Field,  Arr2Field, 
    NumberOfFields 
} TableFields; 

現在您可以輕鬆識別委託回調和方法中的文本字段。我建議保留對伊娃中編輯過的文本字段的引用。您可以在標準的UITextField委託回調中進行設置。

@implementation ViewController { 
    UITextField *_textFieldBeingEdited; 
} 
//... 

「下一步」 和 「上」 的邏輯現在很容易:

// next 
if (textFieldBeingEdited && textFieldBeingEdited.tag < NumberOfFields-1) { 
    [textFieldBeingEdited resignFirstResponder]; 
    UITextField *nextTextField = 
     (UITextField*)[self.view viewWithTag:textFieldBeingEdited.tag+1]; 
    [nextTextField becomeFirstResponder]; 
} 
+0

您的回覆芒迪表示感謝。我會盡快回復你:) – kupilot

相關問題