UIActivityIndi​​cator显示一瞬间

时间:2016-03-03 21:02:18

标签: ios swift uiactivityindicatorview

我试图在我们的应用程序显示一个旋转活动指示器,当用户点击一个按钮然后显示下一个视图 - 它有点工作,我看到它一瞬间

@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var eventsbtn: UIButton!
@IBOutlet weak var pubsbtn: UIButton!
override func viewWillAppear(animated: Bool) {

    myActivityIndicator.stopAnimating()
    myActivityIndicator.hidden = true
}
override func viewDidLoad() {
    super.viewDidLoad()

    eventsbtn.layer.cornerRadius = 8
    pubsbtn.layer.cornerRadius = 8
}



@IBAction func getEvents(sender: AnyObject) {

    myActivityIndicator.hidden = false
    myActivityIndicator.startAnimating()
}

3 个答案:

答案 0 :(得分:0)

重构您的代码。您无需在viewWillAppear中停止活动指示器。您可以在viewDidLoad中隐藏它。这是代码:

@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView!
@IBOutlet weak var eventsbtn: UIButton!
@IBOutlet weak var pubsbtn: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    eventsbtn.layer.cornerRadius = 8
    pubsbtn.layer.cornerRadius = 8
    myActivityIndicator.hidden = true

}

@IBAction func getEvents(sender: AnyObject) {

    myActivityIndicator.hidden = false
    myActivityIndicator.startAnimating()
}

确保在Interface Builder中正确连接活动指示符的IBOutlet和获取事件的IBAction

答案 1 :(得分:0)

我改变它并且它不起作用,直到我回到导航

@IBOutlet weak var myActivityIndicator: UIActivityIndicatorView! @IBOutlet weak var eventsbtn: UIButton! @IBOutlet weak var pubsbtn: UIButton! override func viewDidLoad() { super.viewDidLoad() myActivityIndicator.stopAnimating() myActivityIndicator.hidden = true eventsbtn.layer.cornerRadius = 8 pubsbtn.layer.cornerRadius = 8 } @IBAction func getEvents(sender: AnyObject) { myActivityIndicator.hidden = false myActivityIndicator.startAnimating() }

答案 2 :(得分:0)

viewDidAppear可能是一个更适合停止动画并隐藏微调器的地方。