UIPickerView不会“捕捉”到行并移出界限

时间:2013-11-12 19:12:05

标签: ios objective-c uipickerview uipopover

我遇到UIPickerView问题。在a tutorial on YT之后,我在点击文本字段后在弹出框中创建了一个UIPickerView。

一切看起来都不错,但UIPickerView有点奇怪。如果我只是点击我要选择的行,它会正常向上/向下滚动到此特定行并进行选择。但是如果我尝试用手指滚动,它就不会像动量一样滚动,并且它不会在任何行上捕捉。它也没有做出选择,但它发出的声音就像它一样。最糟糕的是,选择器似乎并不关心行数,我可以根据需要上下滚动,尽管不再有可选择的行。

这是我的代码。

decayStatesArray 包含四个简单的String对象。 UITextfield和 activeDecayString 用于“donePressed”-methode。

主要功能:

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

0 个答案:

没有答案