2013-07-12 48 views
0

在我的视图控制器我有5个文本字段,4成为第一响应者与键盘和一个与UIPicker,他们都被标记+1(0,1,2 ...)添加上一个和下一个按钮到我的UIPicker的辅助视图?

它恰好如此,成为Picker的第一响应者的textfield是3号,所以它卡在中间。在我的其他领域,我有我的代码设置,以便“下一步”返回按钮将我带到我的下一个字段,但是我一直无法使用选取器取得同样的结果,导致发生这种情况的代码:

-(BOOL) textFieldShouldReturn:(UITextField *) theTextField { 
    if (theTextField == self.textFieldE) {  
     [theTextField resignFirstResponder]; //This of course being the last text field 
    } else { 
     [(UITextField *)[theTextField.superview viewWithTag:theTextField.tag+1] 
     becomeFirstResponder]; 
    } 
    return YES; 
} 

UIPickerView有一个附件视图附加到一个完成按钮,事实上resignFirstResponder,但我需要能够添加prev和下一个按钮,使firstResponder -1.tag或+ 1.tag,因为选择器卡在位置编号3

这是我的选择器代码:

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

    if (textField == self.textOutlet) { 

     self.textOutlet.inputView = _thePicker; 
     self.thePicker.hidden = NO; 

     UIToolbar* toolbar = [[UIToolbar alloc] init]; 
     toolbar.barStyle = UIBarStyleBlackTranslucent; 
     [toolbar sizeToFit]; 

     //to make the done button aligned to the right 
     UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 


     UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                     style:UIBarButtonItemStyleDone target:self 
                     action:@selector(doneClicked:)]; 


     [toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft, doneButton, nil]]; 

     self.textOutlet.inputAccessoryView = toolbar; 
    } 

    } 

,当然还有“完成”按钮的作用:

-(void)doneClicked:(id) sender 
{ 
    [_textOutlet resignFirstResponder]; //hides the pickerView 
} 

感谢提前的帮助!

回答

1

试试看看这个代码。它应该如你所愿地工作。

#define SEGMENTED_CONTROL_TAG 1567 // random 

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

    if (textField == self.textOutlet) { 

     self.textOutlet.inputView = _thePicker; 
     self.thePicker.hidden = NO; 

     UIToolbar* toolbar = [[UIToolbar alloc] init]; 
     toolbar.barStyle = UIBarStyleBlackTranslucent; 
     [toolbar sizeToFit]; 

     UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Previous", @"Next", nil]]; 
     segmentedControl.tag = theTextField.tag + SEGMENTED_CONTROL_TAG; 
     [segmentedControl addTarget:self action:@selector(textFieldContinue:) forControlEvents:UIControlEventValueChanged]; 

     //to make the done button aligned to the right 
     UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 


     UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" 
                     style:UIBarButtonItemStyleDone target:self 
                     action:@selector(doneClicked:)]; 


     [toolbar setItems:@[segmentedControl,flexibleSpaceLeft, doneButton]]; 

     self.textOutlet.inputAccessoryView = toolbar; 
    } 

} 


-(IBAction) textFieldContinue:(UISegmentedControl*)control{ 
    UITextField *textField; 
    if(control.selectedSegmentIndex == 0) { 
     textField = (UITextField*)[self.view viewWithTag:control.tag-1 - SEGMENTED_CONTROL_TAG]; 
    } else if (control.selectedSegmentIndex == 1) { 
     textField = (UITextField*)[self.view viewWithTag:control.tag+1 - SEGMENTED_CONTROL_TAG]; 
    } 
    if(textField) { 
     [textField becomeFirstResponder]; 
    } 
} 
+0

好了,我给了代码尝试,但应用程序崩溃时,我选择与选择器文本字段..我换成'theTextField'与具有选择器输入文本字段的名称。我得到以下调试日志:'2013-07-12 11:34:38.979 calcApp [1519:907] - [UISegmentedControl view]:无法识别的选择器发送到实例0x1dd7bfa0 2013-07-12 11:34:38.981 calcApp [1519 :907] ***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:' - [UISegmentedControl视图]:无法识别的选择发送到实例0x1dd7bfa0' '@Vojtech Vrbka – vzm

+0

你有这个函数在UIViewController或你的自定义UITextField类? –

相关问题