我知道如何将数据传递给新的ViewController。问题是我似乎无法从计时器中传递数据。
以下代码适用于一个ViewController。我尝试了多种方法将代码分成两个视图控制器。我已经尝试将计时器放在SecondViewController中。我试过只传递初始的datePicker。我试过在函数中放置函数。
目前的问题是我无法通过totalSeconds,因为它是在定时器中定义的。如果我在计时器之外初始化函数,比如说0,它只返回一个值0.如果我传递datePicker或日期组件,我在SecondViewController中定义时间间隔时会遇到类似的问题。例如,updateTimer函数将无法识别传递给SecondViewController的ViewDidLoad()的值。
非常感谢任何帮助。我只想将updateTimer()中的字符串放在SecondViewController中。
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 :(得分:0)
@CJW ,
您是否了解协议&代表概念。 您可以使用代理人员轻松解决上述问题。协议
1.在第一个viewcontroller类中创建一个协议
protocol TimerUpdateDelegate {
func updateTotalSeconds(_ iTotalSeconds : Int)
}
2.为委托创建
的变量var delegate : TimerUpdateDelegate?
3.现在在第一个视图控制器的updateTimer
方法中添加以下代码行。
if self.delegate != nil
{self.delegate?.updateTotalSeconds(totalSeconds)}
在上面的代码行中,totalSeconds
将是自我updateTimer
函数中的计算值作为参数。
4.现在第二个viewcontroller类中的导入委托为
class ViewController: UIViewController,TimerUpdateDelegate {}
5.在ViewDidload方法中添加以下行集:
override func viewDidLoad() {
firstViewController.delegate = self
}
6.在第二个类视图控制器中定义TimerUpdateDelegate方法:
func updateTotalSeconds(_ iTotalSeconds : Int)
{
self.finalSeconds = iTotalSeconds
}
我希望这能解决你的问题。