2017-06-12 28 views
0

我知道如何将数据传递给新的ViewController。问题是我似乎无法从计时器内传递数据。如何将计时器中的数据传递给新的ViewController(Swift 3)

以下代码适用于一个ViewController。我尝试了多种方法将代码拆分为两个视图控制器。我已经尝试将计时器放在SecondViewController中。我试过只传递最初的datePicker。我曾尝试将函数放入函数中。

目前的问题是我无法传递totalSeconds,因为它是在定时器内定义的。如果我初始化定时器之外的函数,比如说0,它只是返回一个值0.如果我通过datePicker或date组件,当我在SecondViewController中定义时间间隔时遇到类似的问题。例如,updateTimer函数不会识别传递给SecondViewController的ViewDidLoad()的值。

任何帮助,非常感谢。我只想将字符串放在SecondViewController中的updateTimer()中。

class ViewController: UIViewController { 

    @IBOutlet weak var datePicker: UIDatePicker! 
    @IBOutlet weak var Output: UILabel! 
    @IBOutlet weak var Output4: UILabel! 
    @IBOutlet weak var Output3: UILabel! 
    @IBOutlet weak var Output2: UILabel! 
    @IBOutlet weak var timePicker: UIDatePicker! 
    @IBOutlet weak var Output5: UILabel! 
    @IBOutlet weak var Output6: UILabel! 

    var birthDate = DateComponents() 

    @IBAction func timePickerChanged(_ sender: Any) { 

    let tComponents = Calendar.current.dateComponents([.hour, .minute], from: (sender as AnyObject).date) 
    birthDate.hour = tComponents.hour 
    birthDate.minute = tComponents.minute 
} 

    @IBAction func datePickerChanged(_ sender: Any) { 
    let components = Calendar.current.dateComponents([.year, .month, .day], from: (sender as AnyObject).date) 
    birthDate.year = components.year 
    birthDate.month = components.month 
    birthDate.day = components.day 
} 

    override func viewDidLoad() { 
    super.viewDidLoad() 

    Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.updateTimer), userInfo: nil, repeats: true) 

    // Do any additional setup after loading the view, typically from a nib. 
} 

    func updateTimer(){ 
    let now = Date() 

    let birthDay = userCalendar.date(from: birthDate)! 

    let totalSeconds = Int(now.timeIntervalSince(birthDay)) 

    let totalMinutes = userCalendar.dateComponents([.minute], from: birthDay, to: now) 

    let totalHours = userCalendar.dateComponents([.hour], from: birthDay, to: now) 

    let totalDays = userCalendar.dateComponents([.day], from: birthDay, to: now) 

    let totalWeeks = userCalendar.dateComponents([.weekOfYear],from: birthDay, to: now) 

    let totalMonths = userCalendar.dateComponents([.month], from: birthDay, to: now) 

    Output.text = String(totalSeconds) 
    if totalMinutes.minute != nil { 
     Output2.text = String(totalMinutes.minute!) 
    } 
    if totalHours.hour != nil { 
     Output3.text = String(totalHours.hour!) 
    } 
    if totalDays.day != nil{ 
     Output4.text = String(totalDays.day!) 
    } 
    if totalWeeks.weekOfYear != nil{ 
     Output5.text = String(totalWeeks.weekOfYear!) 
    } 
    if totalMonths.month != nil{ 
     Output6.text = String(totalMonths.month!) 
    } 

} 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
    updateTimer() 
    let secondsController = segue.destination as! SecondViewController 
    secondsController.finalSeconds = totalSeconds 
} 

回答

0
@CJW , 

你知道的协议&代表的概念。 您可以使用代理&协议轻松解决上述问题。

1.创建在第一类的ViewController一个协议作为

protocol TimerUpdateDelegate { 
    func updateTotalSeconds(_ iTotalSeconds : Int) 
} 

2.创建变量委托作为

var delegate : TimerUpdateDelegate? 

3.Now添加下面的代码行中的updateTimer方法第一视图控制器。

 if self.delegate != nil 
     {self.delegate?.updateTotalSeconds(totalSeconds)} 

在代码totalSeconds将在自updateTimer功能作为参数的计算出的值以上的线。

4.Now进口委托在第二的ViewController类作为,

  class ViewController: UIViewController,TimerUpdateDelegate {} 

5.In viewDidLoad方法添加下面一组线:

override func viewDidLoad() { 
    firstViewController.delegate = self 
    } 

6.Define TimerUpdateDelegate方法在你的第二类视图控制器:

func updateTotalSeconds(_ iTotalSeconds : Int) 
      { 
       self.finalSeconds = iTotalSeconds 
      } 

我希望这会解决您的问题。

+0

非常感谢。会尝试。编程和iOS新手,所以我没有听说过协议和代表。 – CJW

相关问题