简体   繁体   中英

Stopwatch fired from action button in seperate ViewController

I am trying to have a stopwatch display hours:minutes:seconds after a action button is pressed in a prev viewcontroller. I am using a segue currently:

First viewcontroller code:

import UIKit

class ViewController: UIViewController {

    var time = 0

    var timer = NSTimer()


    func clock(){
        time++
    }

    @IBAction func Run(sender: AnyObject) {
        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: ("clock"), userInfo: nil, repeats: true)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "sendata1"{
            if let destination = segue.destinationViewController as? View2 {destination.viasegue = "\(time)"
        }

    }

}

Second Viewcontroller code:

import Foundation

import UIKit

class View2: UIViewController {

    @IBOutlet weak var timelabel: UILabel!

    var viasegue = "0"

    override func viewDidLoad() {

        timelabel.text = viasegue

        super.viewDidLoad()
    }
}

There are a few ways of doing this but this is the easiest I know. NSNotificationCenter is a way to pass messages from one View to another by utilizing the operating systems notification center...

ViewController.Swift

class ViewController: UIViewController {

    var time = 0
    var timer = NSTimer()

    func clock(){

        time++

        //posts a notification
        NSNotificationCenter.defaultCenter().postNotificationName("timerValID", object: time)

    }

    @IBAction func Run(sender: AnyObject) {

        timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("clock"), userInfo: nil, repeats: true)
         // if your segue is set up directly you may not
         // need this.
         self.performSegueWithIdentifier("sendata1", sender: self)

    }
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }


    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "sendata1"{
            if let destination = segue.destinationViewController as? View2 {
                destination.viasegue = "\(time)"
            }

        }

    }

}

View2.swift

class View2: UIViewController {

    @IBOutlet weak var timelabel: UILabel!

    var viasegue = "0"

    override func viewDidLoad() {
        super.viewDidLoad()
        timelabel.text = viasegue
        // sets up a receiver for your notifications
        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("timerVal:"), name: "timerValID", object: self.view.window)

    }

    // fires each time you get a notification
    func timerVal(notification: NSNotification) {
        guard let time = notification.object else {
            return // or throw
        }

        timelabel.text = "\(time)"
    }

}

update based on comment:

You could manually convert your time to hh mm ss by doing this:

func timerVal(notification: NSNotification) {
    guard let time = notification.object else {
        return // or throw
    }


    timelabel.text = timeFormatted(time as! Int)
}

func timeFormatted(totalSeconds: Int) -> String {
    let seconds: Int = totalSeconds % 60
    let minutes: Int = (totalSeconds / 60) % 60
    let hours: Int = totalSeconds / 3600
    return String(format: "%02d:%02d:%02d", hours, minutes, seconds)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM