gpt4 book ai didi

Swift - 实现全局事件指标

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:03 24 4
gpt4 key购买 nike

我目前正尝试在 swift 中制作一个全局事件指示器,每当对 api 进行提取时都会调用它。这个想法是事件指示器将出现在导航栏(导航 View Controller 的子级)的左上角,并且在每个应用程序页面上都可用。

由于新帐户/低代表而无法显示示例图片

我的事件指示器显示正确,我只是不确定如何在应用程序的任何页面上提供它 - 我考虑过扩展,但不确定最好的方法是什么.任何帮助将不胜感激。

事件指标代码:

let activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.White)
var activityItem = UIBarButtonItem()

func navBarActivity() {
// Call navBarActivity() to start activity indicator
// Use navigationItem.leftBarButtonItem = nil to stop activity indicator
activityIndicator.startAnimating()
activityIndicator.hidden = false

self.activityItem = UIBarButtonItem(customView: activityIndicator)
self.navigationItem.leftBarButtonItem = activityItem
}

最佳答案

我会推荐这样的扩展。

extension UIViewController {
func displayNavBarActivity() {
let indicator = UIActivityIndicatorView(activityIndicatorStyle: .White)
indicator.startAnimating()
let item = UIBarButtonItem(customView: indicator)

self.navigationItem.leftBarButtonItem = item
}

func dismissNavBarActivity() {
self.navigationItem.leftBarButtonItem = nil
}
}

在api调用前调用self.displayNavBarActivity(),在api调用完成后调用self.dismissNavBarActivity()

但是,我希望您检查 UIApplicationnetworkActivityIndi​​catorVisible。考虑使用此选项。

Specify YES if the app should show network activity and NO if it should not. The default value is NO. A spinning indicator in the status bar shows network activity. The app may explicitly hide or show this indicator.

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/#//apple_ref/occ/instp/UIApplication/networkActivityIndicatorVisible

关于Swift - 实现全局事件指标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31183316/

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