Swift/iOS - 多个 android 下拉列表的替代方案是什么

Swift/iOS - Whats the alternative to multiple android drop down lists

我是 iOS 和 Swift 的新手。几年来我一直在制作 android 应用程序。我正在学习 Swift 并为 iOS 设备制作它。

我需要有关下拉列表替代方法的帮助,因为那是我在 android 中使用的方法。

我一直在研究 UIPickerViews、具有 table 视图的 UIPopovers 等等。

Twist 是,我需要在一个下拉列表中选择多个 ViewController。

这是我的 android 应用程序中的视图,我希望在我的 iOS 版本中看起来非常相似。

过去几个月我一直在构建调查应用程序,这个确切的问题已经出现好几次了。虽然 UIPickerView 大多数情况下是有意义的,但它不允许用户一次看到的不仅仅是几个选项,而且它不能很好地从 iPhone 4s 扩展到 6+ 或 iPad。

我建议的第一件事是为下拉菜单创建一个按钮。我发现几乎任何看起来像远程按钮的东西都可以很好地工作,但从用户体验的角度来看,让它看起来像一个超链接或与周围的东西过于内联就不太好用了。

这是我使用的按钮的样子:

就是一个。 UIButton 周围有一个细边框,角半径约为 5。

至于实际的下拉菜单,我发现弹出窗口通常非常有效。对我来说最简单的实现是使用 UIAlertController。 (这仅在 iOS 8+ 中可用,但如果你在 swift 中工作应该没问题。)使用 UIAlertController 你可以有很多选项,一个取消按钮易于访问,标题,消息都以非常标准的方式呈现给用户。您可以在创建控制器时为每个按钮设置操作,这样您就不必与委托一起工作,从而使您的代码更简洁。

这是我之前示例的警报控制器的样子。这是在横向 iPhone 5s 上,这是可能的最小布局,但它会根据任何屏幕尺寸的需要自动放大,并为用户提供类似的体验。

当用户选择了答案后,更新按钮文本以匹配新答案。

我没有使用 UIAlertController,而是使用 table 视图创建了自己的自定义弹出窗口,但我发现自己基本上只是为了它的外观而重新创建警报控制器并找到了它在每个设备上获得恰到好处的布局比值得更困难。话虽如此,UIAlertController 几乎没有提供任何自定义外观的方法,这对某些人来说可能是一个交易破坏者。

我觉得我应该补充一点,我使用 UIAlertController 而不是 UIPickerView 的原因是有意的,因为它允许用户同时看到更多选项并利用可用的屏幕尺寸在 iPad 和更大的 iPhone 上。此外,做出选择后的反馈是即时的,因为一旦做出选择,视图就会消失,Cancel 是一个标准动作,它允许用户快速 而不是 选择,如果只有几个选项,则不需要用户滚动,这意味着只需轻按一下即可进行选择。

这是我用来生成按钮和对应的代码 UIAlertController:

if (self.question.placeholderText) {
    [self.answerButton setTitle:NSLocalizedString(self.question.placeholderText, @"") forState:UIControlStateNormal];
} else {
    [self.answerButton setTitle:NSLocalizedString(@"Please Select One", nil) forState:UIControlStateNormal];
}
[self.answerButton setTitle:[self paddedString:self.answerButton.titleLabel.text] forState:UIControlStateNormal];
self.answerButton.titleLabel.textAlignment = NSTextAlignmentLeft;
self.answerButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
self.answerButton.titleLabel.font = [UIFont appFont];
[self.answerButton setTitleColor:[UIColor appColor] forState:UIControlStateNormal];
[self.answerButton addTarget:self action:@selector(longListLabelTapped) forControlEvents:UIControlEventTouchUpInside];
self.answerButton.backgroundColor = [UIColor colorWithWhite:0.0f alpha:0.0001f];
self.answerButton.titleLabel.layer.borderColor = [UIColor appColor].CGColor;
self.answerButton.titleLabel.layer.borderWidth = 1.0f;
self.answerButton.titleLabel.layer.cornerRadius = 5.0f;

- (NSString *)paddedString:(NSString *)input {
    //This adds some space around the button title just to make it look better
    return [NSString stringWithFormat:@"  %@  ", input];
}

要创建 UIAlertController,您需要一系列选项。

_optionsController = [UIAlertController
                      alertControllerWithTitle:self.question.longListTitle
                      message:nil
                      preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {

}];
[_optionsController addAction:cancelAction];


for (NSString *optionName in self.question.possibleAnswers) {
    UIAlertAction *action = [UIAlertAction actionWithTitle:optionName
                                                     style:UIAlertActionStyleDefault
                                                   handler:^(UIAlertAction *action) {
                                                       // Do something with the selected answer
                                                       [self.answerButton setTitle:[self paddedString:optionName] 
                                                                          forState:UIControlStateNormal];
                                                   }];
    [_optionsController addAction:action];
}