2013-02-16 164 views
0

我已經安裝包含兩個字段的視圖控制器。我在鍵盤上實現了一個工具欄,其中包含「上一個」和「下一個」按鈕。如何使用下一頁按鈕轉到下一個字段

我有一個字段列表中的數組。

// Setup array of listed fields 
textFields = [[NSArray alloc] initWithObjects:usernameField,passwordField, nil]; 

工具欄

(id)initWithTextField:(UITextField*)textField 
{ 
    self = [super initWithFrame:CGRectMake(0, 250, 320, 40)]; 
    if (self) { 

     inputToolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)]; 
     doneButton= [[UIBarButtonItem alloc] 
        initWithTitle:@"Done" 
        style:UIBarButtonItemStyleDone 
        target:self 
        action:@selector(performdone:)]; 
     nextButton= [[UIBarButtonItem alloc] 
        initWithTitle:@"Next" 
        style:UIBarButtonItemStyleBordered 
        target:self 
        action:@selector(nextButtonMethod:)]; 
     nextButton.tintColor = [UIColor blackColor]; 
     previousButton= [[UIBarButtonItem alloc] 
        initWithTitle:@"Previous" 
        style:UIBarButtonItemStyleBordered 
        target:self 
        action:@selector(previousButtonMethod:)]; 
     previousButton.tintColor = [UIColor blackColor]; 

     inputToolBar.barStyle=UIBarStyleDefault; 
     UIBarButtonItem* flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

     [inputToolBar setItems:[NSArray arrayWithObjects:previousButton,nextButton,flexSpace,doneButton,Nil] animated:NO]; 
     [self addSubview:inputToolBar]; 
    } 
    return self; 
} 

現場調用工具欄:

[self.usernameField setInputAccessoryView:myInputAccessoryView]; 

我想知道在我的方法nextButtonMethod和previousButtonMethod添加要得到光標去數組列表中的下一個字段。我怎樣才能做到這一點?

回答

0
[textFieldInstance becomeFirstResponder]; 

將光標置於該字段上。希望你知道如何選擇正確的文本域實例。

0

這麼一件事我可能會建議是建立一個視圖控制器創建廈門國際銀行,並使用內置的也是UITextField的setInputAcessoryView。

在我製作的示例中,我製作了一個帶有xib的自定義視圖控制器,並將其設置爲我想要的按鈕,然後將該視圖加載到InputAccessoryView中 通過以這種方式加載,您可以讓按鈕具有動畫效果當它辭職或成爲第一響應者時,用鍵盤向上和向下移動。

-(void)createFunction{ 

    [myCustomTextField setDelegate:self]; 
    [myCustomTextField setInputView:nil]; 
    //Don't have to set this unless you have a custom view 
    [myCustomTextField setInputAccessoryView:myCustomInputButtonViewController.view]; 

} 

然後我從我的AccessoryView視圖控制器設置委託方法。所以,當按下按鈕時,我可以得到輸入並作出相應的響應。

-(void)pressedLeftActionButtonFromEXT{ 
NSLog(@"Pressed Left Action"); 
NSInteger nextTag = activeTextField.tag - 1; 
// Try to find next responder 
UIResponder* nextResponder = [activeTextField.superview viewWithTag:nextTag]; 
if (nextResponder) { 
    // Found next responder, so set it. 
    [nextResponder becomeFirstResponder]; 
    } else { 
    // Not found, so remove keyboard. 
    [activeTextField resignFirstResponder]; 
    } 

} 

這是我製作的示例項目中的代碼的確切代碼行。你可以想象其他方向恰恰相反。

而且我在接口有一個的UITextField * activeTextField,這樣我可以告訴文本框時,我是在和作出相應的反應。

我希望這會有所幫助。如果這不是讓我知道,我會盡力澄清。我知道我可以解決你遇到的這個問題。

好連接線^^

相關問題