gpt4 book ai didi

ios - swift 中的 alertControllers

转载 作者:行者123 更新时间:2023-11-28 07:03:30 25 4
gpt4 key购买 nike

我在使用 swift 警报 Controller 时遇到了一些问题。我有两个显示事件指标的功能。 1 个有动画,一个没有。创建第二个没有动画的原因是因为.. 当用户单击 TableView 单元格并被切换到新 Controller 时,我正在 View Controller 上显示一个事件。该 Controller 调用网络服务并填充第二个 TableView 。

我的问题是网络服务返回响应的速度太快,以至于当我试图关闭它时事件指示器没有出现在屏幕上,即在网络服务调用的响应中。我在 viewdidload 中显示此指示器,然后在加载后的 View 中调用 Web 服务函数。

我能解决这个问题的唯一方法是创建一个没有动画的事件警报,因为它看起来好像动画正在减慢它的速度。但是当我将动画属性设置为 false 时,警报 Controller 没有 backgroundColor。当我尝试向警报 Controller 添加背景颜色时,宽度变为全屏。

所以我在寻找:

A) 一种在网络服务返回太快时关闭常规警报 Controller 的方法

B) 减小没有动画的第二个警报 Controller 的大小。

提前致谢。我在解雇这些警报 Controller 时遇到了很多麻烦,因为当我试图解雇它们时,我的实际 View Controller 被解雇了,所以我试图检查 presentedController 的类,并且仅在该类是 alertController 时才解散,但我没有不要认为这实际上是绕过它的正确方法。

代码如下:

func displayActivityAlert(title: String, #ViewController: UIViewController)
{
let pending = UIAlertController(title: "\n\n\n"+title, message: nil, preferredStyle: .Alert)
//create an activity indicator
let indicator = UIActivityIndicatorView(frame: pending.view.bounds)
indicator.autoresizingMask = .FlexibleWidth | .FlexibleHeight
indicator.color = UIColor(rgba: Palette.accent)
//add the activity indicator as a subview of the alert controller's view
pending.view.addSubview(indicator)
//pending.view.backgroundColor = UIColor.whiteColor()
indicator.userInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
indicator.startAnimating()


ViewController.presentViewController(pending, animated: true, completion: nil)
}

func displayActivityAlertNoAnim(title: String, #ViewController: UIViewController)
{
let pending = UIAlertController(title: "\n\n\n"+title, message: nil, preferredStyle: .Alert)
//create an activity indicator
let indicator = UIActivityIndicatorView(frame: pending.view.bounds)
indicator.autoresizingMask = .FlexibleWidth | .FlexibleHeight
indicator.color = UIColor(rgba: Palette.accent)
//add the activity indicator as a subview of the alert controller's view
pending.view.addSubview(indicator)

pending.view.backgroundColor = UIColor.whiteColor()
// this line cause the alert controller to become full width of the screen
indicator.userInteractionEnabled = false // required otherwise if there buttons in the UIAlertController you will not be able to press them
indicator.startAnimating()


ViewController.presentViewController(pending, animated: **false**, completion: nil)
}

检查类和解散代码:

if self.presentedViewController!.isKindOfClass(UIAlertController){
self.dismissViewControllerAnimated(true, completion: nil)
}

最佳答案

您需要在 presentViewController() 中使用 completion 参数。这是一个闭包,它将在 UIAlertController 在屏幕上可见后立即执行。


现在,我只能为您提供一些伪代码,因为您没有提供有关如何下载或下载后收到的回调的任何代码,但请尝试以下内容:

func displayActivityAlert(title: String, #ViewController: UIViewController) {
let pending = UIAlertController(title: "\n\n\n"+title, message: nil, preferredStyle: .Alert)
...

ViewController.presentViewController(pending, animated: true) { () -> Void in
// Start downloading from webservice
}
}

并解雇:

if self.presentedViewController!.isKindOfClass(UIAlertController){
self.dismissViewControllerAnimated(true) { () -> Void in
// Perform segue to tableview
}
}

更新 1:

根据 OP 的架构更新了伪代码。


如果您已将警报代码分解到一个单独的文件中,则只需将完成处理程序作为参数传递,如下所示:

func displayActivityAlert(title: String, #ViewController: UIViewController, completionHandler: ()->() ) {
let pending = UIAlertController(title: "\n\n\n"+title, message: nil, preferredStyle: .Alert)
...

self.presentViewController(pending, animated: true, completion: completionHandler)
}

然后每当您调用 displayActivityAlert 时,只需指定回调,例如:

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
displayActivityAlert("Hello", ViewController: self) { () -> () in
// Download from webservice
}
}

关于ios - swift 中的 alertControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31425707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com