gpt4 book ai didi

ios - 将上一个和下一个按钮添加到 UIPicker 的附件 View 中?

转载 作者:行者123 更新时间:2023-11-29 03:47:06 30 4
gpt4 key购买 nike

在我的 View Controller 中,我有 5 个文本字段,其中 4 个成为带键盘的第一响应者,一个成为 UIPicker,它们都被标记为 +1 (0,1,2...)

碰巧成为选择器第一响应者的文本字段是数字 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 有一个附加 View ,带有一个完成按钮,这实际上是 resignFirstResponder 但我需要能够添加一个上一个和下一个按钮以使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
}

感谢您提前提供的帮助!

最佳答案

试试这个代码。它应该按您想要的方式工作。

#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];
}
}

关于ios - 将上一个和下一个按钮添加到 UIPicker 的附件 View 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17616635/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com