设置选择器视图内容时出现问题

时间:2016-04-06 14:51:24

标签: ios objective-c animation uipickerview

在我的应用中,用户可以通过单击某些按钮来更改选择器视图元素的数量。一切都很好。但在那之后我想在其中显示一个单词,但是选择器视图可能还没准备好,应用程序只是崩溃了。这是鳕鱼

- (IBAction)changeNumberOfLettersToShow:(id)sender {
    int numberOfLetters = (int)((UIBarButtonItem *)sender).tag;
    switch (numberOfLetters) {
    case 0:
        [self setNumberOfLetters:[NSNumber numberWithInt: 10] andLanguage:nil];
        break;
    default:
        [self setNumberOfLetters:[NSNumber numberWithInt: numberOfLetters] andLanguage:nil];
        break;
    }
    [self showTheFirstWordForNumberOfLetters:numberOfLetters];
}

- (void) showTheFirstWordForNumberOfLetters: (int) numberOfLetters{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:numberOfLetters], [NSNumber numberWithInt:0], nil] forKey:@"currentPositon"];
    NSArray *words = [allWordsForCurrentLanguage objectForKey:[NSString stringWithFormat:@"%@%d",[[NSUserDefaults standardUserDefaults] objectForKey:@"language"], numberOfLetters]];
    [self displayWord:[[words objectAtIndex:0] uppercaseString] animated:YES];
}

- (void) setNumberOfLetters:(NSNumber *)numberOfLetters andLanguage: (NSString *) language {
    [pickerHelper setNumberOfLettersToShow:numberOfLetters andLanguage:nil];
    [self.picker reloadAllComponents];
}

- (void) displayWord:(NSString *)word animated:(BOOL)animated {
    pickerHelper.pickerWorkingAutomatically = YES;
    for (NSUInteger i = 0; i < [word length]; i++) {
        NSString *letter = [NSString stringWithFormat:@"%c", [word characterAtIndex:i]];
        [self displayLetter:letter
            atComponent:i
               animated:animated];
    }
    pickerHelper.pickerWorkingAutomatically = NO;
}

选择器看起来类似于图片上的选择器: http://sourcefreeze.com/wp-content/uploads/2015/03/uipickerview-multi-selected-row.png

所以我实际上想从包含字母的选择器的片段中创建一个单词(在我开始这样做之前用户也可以正常工作)并且它实际上在模拟器上工作。

更新:

- (void) displayLetter:(NSString *)letter atComponent:(NSUInteger)component animated:(BOOL)animated {
    NSUInteger row;
    if (animated){
        NSUInteger selectedRow = [self.picker selectedRowInComponent:component];
        row = selectedRow + ([self.alphabet count] - selectedRow % [self.alphabet count]) + [self.alphabet indexOfObject:letter];
    }
    else
        row = ((UINT16_MAX) / (2 * [self.alphabet count]) * [self.alphabet count]) + [self.alphabet indexOfObject:letter];

    [self.picker selectRow:row
               inComponent:component
                  animated:animated];
}

0 个答案:

没有答案