alertview
Sadly, one of the drawbacks that they didn’t fix with SwiftUI 2.0 is the AlertView
, which remains somewhat simplistic. Here’s the template code to create a basic one:
遗憾的是,它们无法使用SwiftUI 2.0修复的缺点之一是AlertView
,它仍然有些简化。 这是创建基本代码的模板代码:
That gives us this:
这给了我们这个:
However, most alertView
s need to do more than warn you that the app has done something — they need more buttons. Now, even in SwiftUI 1.0 you can add two — a primary and a secondary button, along with actions.
但是,大多数alertView
需要警告您该应用程序已完成某件事,还需要做更多的事情-他们需要更多的按钮。 现在,即使在SwiftUI 1.0中,您也可以添加两个按钮-一个主按钮和一个辅助按钮以及操作。
Which looks like this:
看起来像这样:
So far, so good. But you know how it is — your client, of course, wants more. And more you cannot do with the standard alertView
in SwiftUI. You need a different solution. What are your options? Staying with SwiftUI we can use an actionSheet
, like this:
到目前为止,一切都很好。 但是您知道情况如何–您的客户当然想要更多。 而且,您不能使用alertView
中的标准alertView
进行更多操作。 您需要其他解决方案。 您有什么选择? 与SwiftUI一起,我们可以使用actionSheet
,如下所示:
Which looks like this:
看起来像这样:
How many actions can you add? As I write this, I’m not sure. I had imagined there would be some sensible limit. I tried adding sixteen possibilities [which is daft] and it simply scrolls. So it seems limitless, but it isn’t a UIAlertViewController
and you know your client will want more.
您可以添加多少个动作? 在我撰写本文时,我不确定。 我曾想过会有一个合理的限制。 我尝试添加16种可能性(很愚蠢),并且它只是滚动。 因此,它似乎是无限的,但它不是UIAlertViewController
,您知道您的客户端会想要更多。
But before we give up, we have a few more tricks in SwiftUI. We can go with a custom alertView
. Here’s the code for one of these with a switch in it. Obviously the sky’s the limit on this, you can add whatever controls you can imagine/build.
但是在放弃之前,SwiftUI还有一些技巧。 我们可以使用自定义的alertView
。 这是其中一个带有开关的代码。 显然,这是天空的极限,您可以添加可以想象/构建的任何控件。
That looks like this:
看起来像这样:
Of course, you can also make this view appear on the screen using different transitions. Here’s a version that looks much like the action sheet, only this is a custom view.
当然,您也可以使用不同的过渡使该视图显示在屏幕上。 这是一个看起来类似于操作表的版本,只是一个自定义视图。
But hey, you know it’s also possible to use an UIAlertViewController
. Just in case he already sold one and all else fails, all is not lost. Here’s the code to do that in SwiftUI:
但是,您知道也可以使用UIAlertViewController
。 万一他已经卖掉了,而其他所有都失败了,一切都不会丢失。 这是在SwiftUI中执行此操作的代码:
Here’s a reminder of what that looks like:
这是对外观的提醒:
And with that, I bring this article to its conclusion.
这样,我便得出本文的结论。
Keep calm, keep coding!
保持冷静,保持编码!
翻译自: https://medium.com/better-programming/7-alertview-options-in-swiftui-691ccb742d22
alertview