秒表在单独的ViewController中从动作按钮触发

时间:2016-01-03 17:28:39

标签: ios swift uiviewcontroller

我正在尝试让秒表显示小时:在prev viewcontroller中按下操作按钮后的几分钟:秒。我目前正在使用segue:

第一个视图控制器代码:

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)"
        }

    }

}

第二个Viewcontroller代码:

import Foundation

import UIKit

class View2: UIViewController {

    @IBOutlet weak var timelabel: UILabel!

    var viasegue = "0"

    override func viewDidLoad() {

        timelabel.text = viasegue

        super.viewDidLoad()
    }
}

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到这一点,但这是我所知道的最简单的方法。 NSNotificationCenter是一种利用操作系统通知中心将消息从一个视图传递到另一个视图的方法...

<强> 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)"
    }

}

根据评论进行更新:

您可以通过以下方式手动将时间转换为hh mm ss:

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)
}