问题描述
我在swift IOS中有两个 ViewControllers
(A和B)。 A和B都从互联网(单独)加载数据。我想在加载时显示 activityIndicator
。我知道我可以通过在每个VC中声明一次来做坏事,如下所示
I have two ViewControllers
(A and B) in swift IOS. Both A and B loads data from internet (separately). I want to display activityIndicator
while loading. I know I can do it the bad way by declaring it once in each VC as follows
ViewController A
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
func activityIndicatorBegin() {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
disableUserInteraction()
greyView = UIView()
greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
greyView.backgroundColor = UIColor.blackColor()
greyView.alpha = 0.5
self.view.addSubview(greyView)
}
func activityIndicatorEnd() {
self.activityIndicator.stopAnimating()
enableUserInteraction()
self.greyView.removeFromSuperview()
}
并对ViewController B执行完全相同的操作,并调用在B视图控制器中声明的activityIndicatorBegin和activityIndicatorEnd函数。但是,我想让代码更清晰。怎么可能呢?我这些天试图使代码更清洁。
and do the exact same for ViewController B and call the activityIndicatorBegin and activityIndicatorEnd functions that is declared in B view controller. However, I want to make the code cleaner. How could it be done? I am trying to make code cleaner these days.
谢谢,
- 更新 -
我会像以下代码一样成像。但它并不是因为我无法在扩展中声明变量
I would imageing something like the following code would work. But it doesnt because I cant declare variables in extensions
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
extension UIViewController {
func activityIndicatorBegin() {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
disableUserInteraction()
greyView = UIView()
greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
greyView.backgroundColor = UIColor.blackColor()
greyView.alpha = 0.5
self.view.addSubview(greyView)
}
func activityIndicatorEnd() {
self.activityIndicator.stopAnimating()
enableUserInteraction()
self.greyView.removeFromSuperview()
}
}
推荐答案
创建一个 BaseViewController
并添加这两个方法并声明 activityIndicator
BaseViewController
中的对象。现在将 BaseViewController
作为所有 ViewController
的父类。之后,如果你想添加指标,只需像这样调用
Create one BaseViewController
and add these two method and declare activityIndicator
object inside that BaseViewController
. Now make BaseViewController
as parent class of all your ViewController
. After that if you want to add the indicator simply call like this
1)创建BaseViewController
1) Create BaseViewController
class BaseViewController: UIViewController {
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func activityIndicatorBegin() {
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = self.view.center
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
view.addSubview(activityIndicator)
activityIndicator.startAnimating()
disableUserInteraction()
greyView = UIView()
greyView.frame = CGRectMake(0, 0, self.view.bounds.width, self.view.bounds.height)
greyView.backgroundColor = UIColor.blackColor()
greyView.alpha = 0.5
self.view.addSubview(greyView)
}
func activityIndicatorEnd() {
self.activityIndicator.stopAnimating()
enableUserInteraction()
self.greyView.removeFromSuperview()
}
}
2)现在将此 BaseViewController
指定为所有 viewController
的父级,如下所示
2) Now assign this BaseViewController
as parent of your all viewController
like this
class ViewController1: BaseViewController {
override func viewDidLoad() {
super.viewDidLoad()
//Now if you want to add activityIndicator call like this
self.activityIndicatorBegin()
//or if you want remove activityIndicator
self.activityIndicatorEnd()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
这篇关于在许多VC中使用活动指示器而不重复代码swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!