我试图在我的代码中实现KNMultiItemSelector以选择数组中的多个项目。我一直试图让选择器显示一些在初始化时被选中的项目,但是它似乎不起作用。我的代码如下:

for (Option* option in optionssList) {
        NSString* name = [NSString stringWithFormat:@"%@", option.name];
        KNSelectorItem* item = [[KNSelectorItem alloc] initWithDisplayValue:name selectValue:option.id imageUrl:nil];
        [items addObject:item];
        if ([_selectedOptions containsObject:item]) {
            NSLog(@"Already selected");
            [_selectedOptions addObject:item];
        }
    }

    KNMultiItemSelector * selector = [[KNMultiItemSelector alloc] initWithItems:items
                                                               preselectedItems:_selectedOptions
                                                                          title:@"List"
                                                                placeholderText:@"Select Options"
                                                                       delegate:self];

    selector.useRecentItems = YES;
    selector.maxNumberOfRecentItems = 5;

    UINavigationController * uinav = [[UINavigationController alloc] initWithRootViewController:selector];
    uinav.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; // iPhone
    uinav.modalPresentationStyle = UIModalPresentationFormSheet;       // iPad only
    [self presentViewController:uinav animated:YES completion:nil];


任何帮助,将不胜感激。谢谢!

最佳答案

您正在KNMultiItemSelector中遇到一个错误:KNMultiItemSelector初始化程序忽略preselectedItems。要解决该错误,请在要选择的项目上将selected设置为YES,并确保preselectedItems不为零。 (它不必包含预选的项目。)例如:

KNSelectorItem *item = [[KNSelectorItem alloc] initWithDisplayValue:@"Item"];
item.selected = YES;

KNMultiItemSelector *selector = [[KNMultiItemSelector alloc] initWithItems:@[item]
    preselectedItems:@[]
    title:@"List"
    placeholderText:@"Select Options"
    delegate:self];

09-30 22:08