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