0

我有许多文本字段,它们以选择器视图的形式提供预定义值的选择。有时,通常在填充了很多文本框后,选择器变黑,然后应用程序崩溃。

我似乎无法重现该问题,它只是偶尔发生......由一些用户报告。当我从别人那里拿到这个代码时,也许我错过了什么?

这是文本框/选择器的自定义类的代码

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.touchEnabled = YES;
        self.textField = nil;
        self.editableTextField = nil;
        __selectedValue = @"";
        self.pickerView = nil;
        self.button = nil;
        self.textValue = nil;
        self.otherSelected = NO;
        self.backgroundColor = [UIColor clearColor];
        self.userInteractionEnabled = YES;
        self.values = [NSArray arrayWithObjects:@"Value1", @"Value2", @"Other", nil];
    }
    return self;
}

- (void)layoutSubviews
{
    if (self.textField == nil) {
        UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 31.0f)];
        textField.borderStyle = UITextBorderStyleRoundedRect;
        textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
        textField.font = [UIFont systemFontOfSize:12.0f];

        UIPickerView *pickerView = [[UIPickerView alloc] init];
        [pickerView sizeToFit];
        pickerView.delegate = self;
        pickerView.dataSource = self;
        pickerView.showsSelectionIndicator = YES;
        if (self.textValue != nil && ![self.textValue isEqualToString:@""]) {
            textField.text = self.textValue;
            int matchedIndex = -1;
            int otherIndex = -1;
            for (int i = 0; i < self.values.count; i++) {
                NSString *value = [self.values objectAtIndex:i];
                if ([value isEqualToString:@"Other"]) {
                    otherIndex = i;
                }
                if ([self.textValue isEqualToString:[self.values objectAtIndex:i]]) {
                    matchedIndex = i;
                }
            }
            if (matchedIndex > -1) {
                [pickerView selectRow:matchedIndex inComponent:0 animated:NO];
            } else if (otherIndex > -1) {
                [pickerView selectRow:otherIndex inComponent:0 animated:NO];
            }
        }
        textField.inputView = pickerView;
        self.pickerView = pickerView;

        UIToolbar *accessoryView = [[UIToolbar alloc] init];
        [accessoryView sizeToFit];
        accessoryView.barStyle = UIBarStyleBlackOpaque;
        accessoryView.tintColor = [UIColor grayColor];
        UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
        UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(pickerDone:)];
        self.doneButton = doneButton;
        accessoryView.items = [NSArray arrayWithObjects:spacer, doneButton, nil];
        textField.inputAccessoryView = accessoryView;

        [self addSubview:textField];
        self.textField = textField;
    }

    if (self.button == nil) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor clearColor];
        button.frame = self.bounds;
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        self.button = button;
    }
}

- (void)buttonPressed:(id)sender
{
    if (self.fieldDelegate != nil) {
        [self.fieldDelegate performSelector:@selector(fieldBeganEditing:) withObject:self];
    }
    if (self.otherSelected) {
        self.doneButton.action = @selector(textFieldDone:);
        self.textField.inputView = nil;
    } else {
        self.doneButton.action = @selector(pickerDone:);
        self.textField.inputView = self.pickerView;
    }
    [self.textField becomeFirstResponder];
}

- (void)textFieldDone:(id)sender
{
    LogCmd();
    [self.textField resignFirstResponder];
    self.textField.inputView = self.pickerView;
    self.doneButton.action = @selector(pickerDone:);
    [self.textField becomeFirstResponder];
}

- (void)pickerDone:(id)sender
{
    LogCmd();
    NSString *value = [self.values objectAtIndex:[self.pickerView selectedRowInComponent:0]];
    if (!self.otherSelected) {
        self.textField.text = value;
        __selectedValue = value;
    }
    [self.textField resignFirstResponder];
}

/*- (void)setValues:(NSArray *)values
 {
 _values = values;
 [self.pickerView reloadAllComponents];
 }*/

#pragma mark - UITextFieldDelegate

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    LogCmd();
}

#pragma mark - UIPickerViewDelegate

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:      (NSInteger)component
{
    NSString *value = [self.values objectAtIndex:row];
    if ([value isEqualToString:@"Other"]) {
        self.otherSelected = YES;
        self.textField.text = @"";
        __selectedValue = @"";

        [self.doneButton setAction:@selector(textFieldDone:)];       
        self.textField.inputView = nil;
        [self.textField resignFirstResponder];
        [self.textField becomeFirstResponder];
        //[self.textField performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0.2];
    } else {
        self.otherSelected = NO;
        self.textField.text = value;
        __selectedValue = value;
    }
}

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

#pragma mark - UIPickerViewDataSource

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return self.values.count;
}

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

0 回答 0