1

我对 UIPickerView 有疑问。在YT 上的教程之后,我在点击文本字段后在弹出窗口中创建了一个 UIPickerView。

一切看起来都很好,但 UIPickerView 的行为有点奇怪。如果我只是点击我想要选择的行,它通常会向上/向下滚动到该特定行并进行选择。但是,如果我尝试用手指滚动,它就不会像应有的那样平滑滚动,并且不会在任何行上对齐。它也不会进行选择,但会发出类似的声音。最糟糕的是,选择器似乎并不关心行数,我可以随心所欲地上下滚动,尽管不再有可选择的行。

这是我的代码。

decayStatesArray包含四个简单的字符串对象。UITextfield 和activeDecayString用于“donePressed”方法。

主要功能:

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

activeTextfield = textField;
//for the default sizes of a UIPickerView
decayStatePicker = [[UIPickerView alloc] init];

UIViewController *popOverContent = [[UIViewController alloc] init];
UIView *popOverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [decayStatePicker frame].size.width, [decayStatePicker frame].size.height+44)];
decayStatePicker = [[UIPickerView alloc] initWithFrame:CGRectMake([popOverView frame].origin.x, [popOverView frame].origin.y + 44, 0, 0)];
pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake([popOverView frame].origin.x, [popOverView frame].origin.y, [popOverView frame].size.width, 44)];

[decayStatePicker setDataSource:self];
[decayStatePicker setDelegate:self];
[decayStatePicker setShowsSelectionIndicator:YES];
[decayStatePicker selectRow:0 inComponent:0 animated:YES];

//Create Toolbar
[pickerToolbar setBarStyle:UIBarStyleBlackTranslucent];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleBordered target:self action:@selector(donePressed)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelPressed)];
[pickerToolbar setItems:[NSArray arrayWithObjects:flexibleSpace, doneButton, cancelButton, nil]];

[popOverView addSubview:decayStatePicker];
[popOverView addSubview:pickerToolbar];
[popOverContent setView:popOverView];

pickerPopoverController = [[UIPopoverController alloc] initWithContentViewController:popOverContent];
pickerPopoverController.popoverContentSize = CGSizeMake([popOverView frame].size.width, [popOverView frame].size.height);
[pickerPopoverController presentPopoverFromRect:[textField frame] inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}

委托方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:  (NSInteger)component {
    return [decayStatesArray count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [decayStatesArray objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if ([pickerView isEqual:decayStatePicker]) {
        NSLog(@"The Picker is: %@", pickerView);
        NSLog(@"The item is: %@", [decayStatesArray objectAtIndex:row]);
    }
    activeDecayString = [decayStatesArray objectAtIndex:row];
}
4

0 回答 0