0

I know how to use pull to refresh and how to make a refresh button. I am just wondering if anyone knows how or can link me to a page that teaches me how auto refresh a page. Like every 15 seconds to automatically refresh.

JCDOSAJ
  • 15
  • 1
  • 6
  • You can use a timer http://stackoverflow.com/questions/24007518/how-can-i-use-nstimer-in-swift – Caleb Aug 06 '15 at 15:12

2 Answers2

0

Start with this:

@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
}
Caleb
  • 5,548
  • 3
  • 25
  • 32
whereisleo
  • 818
  • 5
  • 12
  • My code is trying to do this, and it is running it. But I get thread 1 signal SIGABRT error. Itt says I have an unrecognized selector. – JCDOSAJ Aug 06 '15 at 15:54
  • You commented on both of our answer. If you're using mine, I made a few edits. – whereisleo Aug 06 '15 at 15:58
  • Because I tried both and both gave me the same error haha – JCDOSAJ Aug 06 '15 at 16:01
  • @JCDOSAJ I don't think you need `timer: NSTimer` in the parameters of `refreshEvery15Secs`. It should just be `func refreshEvery15Secs()` The `action` for the `UIRefreshControl` needs a function with `(sender: AnyObject)` as the parameters. – Caleb Aug 06 '15 at 16:17
  • Caleb is right, I wrote it from memory but i made some changes again. – whereisleo Aug 06 '15 at 16:21
  • @JCDOSAJ I made an edit to the answer. The code above should work. – Caleb Aug 06 '15 at 16:38
  • I have all this code, it just isnt firing. I tried putting refresher.beingRefreshing() in the refreshEvery15Secs func but it causes the EXC_BREAKPOINT error. – JCDOSAJ Aug 06 '15 at 17:23
-1

create NSTimer to fire every 15 seconds

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

do the refresh in a callback function

func refreshData(timer: NSTimer){
    refreshControl.beginRefreshing()
}
nRewik
  • 8,958
  • 4
  • 23
  • 30
  • My code is trying to do this, and it is running it. But I get thread 1 signal SIGABRT error. Itt says I have an unrecognized selector. – JCDOSAJ Aug 06 '15 at 15:53