使用 swift 自动刷新

Auto refresh using swift

我知道如何使用拉动刷新以及如何制作刷新按钮。我只是想知道是否有人知道如何或可以 link 我到一个教我如何自动刷新页面的页面。喜欢每隔15秒自动刷新一次。

创建 NSTimer 每 15 秒触发一次

let timer = NSTimer.scheduledTimerWithTimeInterval(15, target: self, selector: "refreshData:", userInfo: nil, repeats: true)

在回调函数中进行刷新

func refreshData(timer: NSTimer){
    refreshControl.beginRefreshing()
}

从这里开始:

@IBOutlet weak var yourButton: UIButton!
var timer: NSTimer!
var refresher: UIRefreshControl!


override func viewDidLoad() {
    super.viewDidLoad()
    yourButton.addTarget(self, action: "refresh:", forControlEvents: .TouchUpInside)
    refresher = UIRefreshControl()
    refresher.addTarget(self, action: "refresh:", forControlEvents: .ValueChanged)

    timer = NSTimer.scheduledTimerWithTimeInterval(15.0, target: self, selector:"refreshEvery15Secs", userInfo: nil, repeats: true)
}

func refreshEvery15Secs(){
    // refresh code
}

func refresh(sender: AnyObject){

     refreshEvery15Secs() // calls when ever button is pressed
}