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

Swift/iOS-多个android下拉列表的替代方案是什么,ios,swift,drop-down-menu,uipickerview,uipopover,Ios,Swift,Drop Down Menu,Uipickerview,Uipopover,我不熟悉iOS和Swift。我已经做了几年安卓应用了。我正在学习Swift,并将其应用于iOS设备 我需要其他下拉列表的帮助,因为这是我在android中使用的 我一直在研究UIPickerViews、带有表视图的UIPopover等等 Twist是,我需要在一个ViewController中有多个备选下拉列表 这是我的android应用程序中的视图,我希望在我的iOS版本中看起来非常相似。在过去的几个月里,我一直在构建调查应用程序,而这个确切的问题已经出现了好几次。虽然UIPickerView

我不熟悉iOS和Swift。我已经做了几年安卓应用了。我正在学习Swift,并将其应用于iOS设备

我需要其他下拉列表的帮助,因为这是我在android中使用的

我一直在研究UIPickerViews、带有表视图的UIPopover等等

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


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

在过去的几个月里,我一直在构建调查应用程序,而这个确切的问题已经出现了好几次。虽然
UIPickerView
在很大程度上是有意义的,但它不允许用户一次只能看到几个选项,而且从iphone4s到6+或iPad的扩展性也不好

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

下面是我使用的按钮的外观:

所有这些都是a。UIButton,周围有薄边框,角半径约为5

至于实际的下拉列表,我发现流行音乐通常是非常有效的。对我来说,最简单的实现是使用
UIAlertController
。(这仅在iOS 8+中可用,但如果您使用swift也可以。)使用
UIAlertController
您可以拥有大量选项、易于访问的取消按钮、标题和消息,所有这些都以非常标准的方式呈现给用户。您可以在创建控制器时设置每个按钮的操作,这样您就不必处理委托,从而使代码更干净

下面是我前面示例中的警报控制器的外观。这是在iPhone 5s中的横向布局,这是可能的最小布局,但它会根据需要自动放大,以适应任何屏幕大小,并为用户提供类似的体验

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

我没有使用
UIAlertController
,而是创建了自己的带有表视图的自定义弹出窗口,但我发现自己基本上只是为了它的外观而重新创建AlertController,并且发现要在每个设备上获得恰到好处的布局比实际情况更困难。话虽如此,
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];
}

这似乎是一个很好的方法。我在我的一个类似的应用程序中看到了这一点,在这个应用程序中,你点击一些看起来像文本字段的东西,但会打开一个AlertController类型的感觉,当选择时,它会在该文本字段中填充该选择的名称文本。谢谢你,伙计。似乎这是最好的方法。我会给它更多的时间,因为我标记为答案,但这似乎很好。在试图从其他平台复制设计时,在《人机界面指南》中提到的一件事经常被忘记,那就是最小按钮大小为44分。创建一个看起来很好的按钮,是一条直线,并且很容易用手指点击是很困难的。不过,Popover基本上解决了这个问题。你认为可以通过电子邮件发送一些关于如何制作的提示和代码吗?我喜欢你的。我需要一些帮助。我刚刚添加了一个代码示例,用于获得我所做的外观。我删除了大部分上下文,但是有很多东西可以让它工作,并且应该很容易实现到您自己的代码中。我还解释了为什么我使用
UIAlertController
而不是
UIPickerView