Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/105.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用UIViewController中的值在UIView中滚动自定义选择器_Ios_Uipickerview - Fatal编程技术网

Ios 使用UIViewController中的值在UIView中滚动自定义选择器

Ios 使用UIViewController中的值在UIView中滚动自定义选择器,ios,uipickerview,Ios,Uipickerview,我有一个UIViewController,带有一个文本字段和一个UIView,其中加载了带有UIPickerView的xib文件 TextField具有作为inputView的UIView 当我开始编辑空文本视图时,选择器选择两个部分中的第一行。选择一些行后,TextField从选择器获取数据。一切都很好 但是如果TextField已经从选择器中获得了一些值,并且我再次开始编辑它,那么选择器将再次选择第一行。我需要将选择器滚动到行,该行包含与TextField相同的值。我怎么做 我正在尝试从Te

我有一个UIViewController,带有一个文本字段和一个UIView,其中加载了带有UIPickerView的xib文件

TextField具有作为inputView的UIView

当我开始编辑空文本视图时,选择器选择两个部分中的第一行。选择一些行后,TextField从选择器获取数据。一切都很好

但是如果TextField已经从选择器中获得了一些值,并且我再次开始编辑它,那么选择器将再次选择第一行。我需要将选择器滚动到行,该行包含与TextField相同的值。我怎么做

我正在尝试从TextField中委托一个字符串值

BasicInfoViewController.m

- (IBAction)startDateBegin:(UITextField *)sender
{
    YBCDatePicker *datePicker = [[YBCDatePicker alloc]init];

    datePicker.datePickerToBasicDelegate = self;

    self.startDateTextField.inputView = datePicker;

    datePicker.delegatedPickerDate = self.startDateTextField.text;
}
- (void)datePicker:(YBCDatePicker *)controller datePicked:(NSString *)datePickerStringDelegate
{
    self.startDateTextField.text = datePickerStringDelegate;
}
日期选择器

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:CGRectMake(0, 0, 320, 216)];

    if (self) {
    UIView *pickerView = [[[NSBundle mainBundle]loadNibNamed:@"DatePickerView" owner:self options:nil]lastObject];
    [self addSubview:pickerView];

        NSLog(@"%@",delegatedPickerDate);
    }
        return self;
}
NSlog返回nil

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    NSLog(@"%@",delegatedPickerDate);

    NSArray *tmpArray = [delegatedPickerDate componentsSeparatedByString:@" "];

    if (tmpArray.count == 2) {

        [self creatingMonths];
        [self creatingYears];

        monthLocation = [months indexOfObject:[tmpArray objectAtIndex:0]];
        yearLocation = [years indexOfObject:[tmpArray objectAtIndex:1]];
        [self.datePickerOutlet selectRow:monthLocation inComponent:0 animated:YES];
        [self.datePickerOutlet selectRow:yearLocation inComponent:1 animated:YES];
    }
}
NSlog返回nil,然后是委托值,然后是nil。选择器没有滚动

但如果我这样做:

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    NSLog(@"%@",delegatedPickerDate);

//  NSArray *tmpArray = [delegatedPickerDate componentsSeparatedByString:@" "];
//   
//    if (tmpArray.count == 2) {
//        
        [self creatingMonths];
        [self creatingYears];
//        
//        monthLocation = [months indexOfObject:[tmpArray objectAtIndex:0]];
//        yearLocation = [years indexOfObject:[tmpArray objectAtIndex:1]];
//        
//    }

    [self.datePickerOutlet selectRow:5 inComponent:0 animated:YES];
    [self.datePickerOutlet selectRow:5 inComponent:1 animated:YES];
}
选择器滚动到该行

当TextField值尚未委托时,选择器似乎获取数据来选择某一行,而当值已委托时,选择器没有滚动

我尝试使用不同的UIView void方法,但没有成功

编辑:

日期选择器

@class YBCDatePicker;
@protocol datePickerDelegate <NSObject>
@required
- (void)datePicker:(YBCDatePicker*)controller datePicked:(NSString*)datePickerStringDelegate;
@end
@interface YBCDatePicker : UIView <UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak,nonatomic)id <datePickerDelegate> datePickerToBasicDelegate;
BasicInfoViewController.m

- (IBAction)startDateBegin:(UITextField *)sender
{
    YBCDatePicker *datePicker = [[YBCDatePicker alloc]init];

    datePicker.datePickerToBasicDelegate = self;

    self.startDateTextField.inputView = datePicker;

    datePicker.delegatedPickerDate = self.startDateTextField.text;
}
- (void)datePicker:(YBCDatePicker *)controller datePicked:(NSString *)datePickerStringDelegate
{
    self.startDateTextField.text = datePickerStringDelegate;
}
附言。 我用另一种方法创建了一个选择器。我刚刚在一个UIViewController.m文件中添加了alloc和init一个没有xib文件和其他UIView类的选择器。所有这些都很好,因为我不需要在不同的.m文件之间委托值


但问题仍然需要解决,因为我想使用一个UIView和一个具有不同ViewController的选择器。

关闭后选择器值重置的原因是您没有全局声明它。您可以通过将以下内容添加到BasicInfoViewController.m中来实现这一点。现在将保存datePicker中存储的所有值

@interface BasicInfoViewController
{ 
    YBCDatePicker *datePicker;
}

- (void)viewDidLoad
{
    datePicker = [[YBCDatePicker alloc] init]; 
}

那么您实际上是想让UITextFieldDelegate方法从UITextField检索字符串?还有,你所说的UIView无效方法是什么意思?@davetw12,不完全是这样。我不能在正确的时间使用接收到的字符串。首先,我需要接收一个字符串,然后使用它来选择行。但现在它倒过来了。我尝试使用不同的方法来接收string和setRow:Incomonent。无效方法didMoveToSuperview、willMoveToWindow等找不到正确的方法。为了确保我正确地跟踪您:第一次使用选择器在文本字段中设置值时,一切都完全按照您希望的方式工作?让我举个例子。文本字段为空。当我开始编辑它时,选择器确实显示了一个默认值-2013年1月。然后我在2015年3月的选择器中选择,文本字段显示该值。一切都很好。然后我点击inputAccessoryView中的按钮以隐藏选择器。拾荒者确实消失了。然后我第二次点击文本字段,选择器在2013年1月再次出现,但我需要选择器在2015年3月出现。从picker到TextField的委托工作正常,但从TextField到picker的委托工作不正确。在picker视图中设置日期后,是否保存日期的值?我使用全局字符串在pickerView:didSelectRow:Incomonent方法中委托它。但当选择器再次出现时,该字符串为零。选择器在UIView中,文本字段在另一个UIViewController中,因此,当选择器消失时,它会清除所有值您所说的“委派它的全局字符串”是什么意思?-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row incomonent:(NSInteger)component{NSInteger monthRow=[pickerView selectedRowInComponent:monthComponent];NSInteger yearRow=[pickerView selectedRowInComponent:yearComponent];monthString=[self.monthRow]对象索引:monthRow];yearString=[self.years对象索引:yearRow];selectedPickerDate=[NSString stringWithFormat:@“%@%”,monthString,yearString];[self.datePickerToBasicDelegate datePicker:self-DatePickerd:selectedPickerDate];}为了便于阅读,您可以将picker委托类中的代码发布到问题的代码区域。您是否可以共享整个项目的下载链接?