異なるファイル間での変数のやりとりについて
上の画像のような違う**.swift**間で変数を共有したいのですがどのようにしたら良いでしょうか
//ViewController.swift
class NextViewController {
var number: Int = 60
}
//TimerManagar.swift
class TimerManagar {
let nextViewController = NextViewController()
var timerNumber = nextViewController.number //エラーCannot use instance member 'nextViewController' within property initializer; property initializers run before 'self' is available
}
本当にやりたいことです↓↓
グチャグチャしたコードで頭がこんがらがると思うのですがよろしくお願いします
僕の頭はもうグチャグチャです。
異なるファイル間での変数のやりとりについて の質問のコードのところにのマークのコメントをつけています
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var characterImage: UIImageView!
@IBOutlet weak var timerlabel: UILabel!
@IBOutlet weak var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
startButton.layer.cornerRadius = 25.0
}
}
import UIKit
class NextViewController: UIViewController {
@IBOutlet weak var countdwonTimer: UILabel!
@IBOutlet weak var executionTimer: UILabel!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var kyannseruButton: UIButton!
@IBOutlet weak var twentyButton: UIButton!
@IBOutlet weak var thirtyButton: UIButton!
@IBOutlet weak var sixtyButton: UIButton!
@IBOutlet weak var ninetyButton: UIButton!
var nowNumber = 0
var timer : Timer?
override func viewDidLoad() {
super.viewDidLoad()
stopButton.layer.cornerRadius = 30.0
kyannseruButton.layer.cornerRadius = 30.0
twentyButton.layer.cornerRadius = 22.5
thirtyButton.layer.cornerRadius = 22.5
sixtyButton.layer.cornerRadius = 22.5
ninetyButton.layer.cornerRadius = 22.5
TimerManager.shared.labelUpDate(TimerManager.shared.count)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
TimerManager.shared.onCountDidSet = { count in TimerManager.shared.labelUpDate( Int(count))}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
TimerManager.shared.onCountDidSet = nil
}
@IBAction func stopButtonAction(_ sender: UIButton) {
if timer?.isValid != true {
if nowNumber == 20 || nowNumber == 30 || nowNumber == 60 || nowNumber == 90 {
if nowNumber == 20 {
//TimerscheduledTimer
} else if nowNumber == 30 {
} else if nowNumber == 60 {
} else if nowNumber == 90 {
}
stopButton.setTitle("stop", for: .normal)
twentyButton.isUserInteractionEnabled = false
thirtyButton.isUserInteractionEnabled = false
sixtyButton.isUserInteractionEnabled = false
ninetyButton.isUserInteractionEnabled = false
}
} else {
if let nowTimer = timer {
if nowTimer.isValid == true {
nowTimer.invalidate()
stopButton.setTitle("start", for: .normal)
twentyButton.isUserInteractionEnabled = true
thirtyButton.isUserInteractionEnabled = true
sixtyButton.isUserInteractionEnabled = true
ninetyButton.isUserInteractionEnabled = true
}
}
}
}
@IBAction func cancelButtonAction(_ sender: UIButton) {
if let nowTimer = timer {
if nowTimer.isValid == true {
nowTimer.invalidate()
}
}
twentyButton.isUserInteractionEnabled = true
thirtyButton.isUserInteractionEnabled = true
sixtyButton.isUserInteractionEnabled = true
ninetyButton.isUserInteractionEnabled = true
nowNumber = 0
TimerManager.shared.labelUpDate(0)
stopButton.setTitle("start", for: .normal)
}
@IBAction func twentyButtonAction(_ sender: UIButton) {
TimerManager.shared.labelUpDate(1200)
nowNumber = 20
}
@IBAction func thertyButtonActiojn(_ sender: UIButton) {
TimerManager.shared.labelUpDate(1800)
nowNumber = 30
}
@IBAction func sixtyButtonAction(_ sender: UIButton) {
TimerManager.shared.labelUpDate(3600)
nowNumber = 60
}
@IBAction func ninetyButtonAction(_ sender: UIButton) {
TimerManager.shared.labelUpDate(5400)
nowNumber = 90
}
}
import UIKit
class TimerManager : UIViewController {
static var shared = TimerManager()
let settingKey = "timer_value"
let settingKeytwenty = "timer_twenty"
let settingKeythirty = "timer_thirty"
let settingKeysixty = "timer_sixty"
let settingKeyninety = "timer_ninety"
var timer : Timer?
var startDate : Date?
var countString = "00 : 00"
var count = 0 {
didSet {
onCountDidSet?(Double(count))
}
}
var onCountDidSet : ((_ count : Double) -> Void)? = nil
let nextViewController = NextViewController() //ここです!!!!!
var timerNowNunber = nextViewController.nowNumber //ここです!!!!!
override func viewDidLoad() {
super.viewDidLoad()
let settings = UserDefaults.standard
settings.register(defaults: [settingKey : 216000])
let settings20 = UserDefaults.standard
settings20.register(defaults: [settingKeytwenty : 1200])
let settings30 = UserDefaults.standard
settings30.register(defaults: [settingKeythirty : 1800])
let settings60 = UserDefaults.standard
settings60.register(defaults: [settingKeysixty : 3600])
let settings90 = UserDefaults.standard
settings90.register(defaults: [settingKeyninety : 5400])
}
func startCount() {
startDate = Date()
if timer == nil {
if timerNowNunber == 20 || timerNowNunber == 30 || timerNowNunber == 60 || timerNowNunber == 90 { //timerNowNunberで処理を分岐したい
if timerNowNunber == 20 {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
self.twntytimer(timer)
})
} else if timerNowNunber == 30 {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: {timer in self.thritytimer(timer)})
} else if timerNowNunber == 60 {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
self.sixtytimer(timer)})
} else if timerNowNunber == 90 {
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
self.ninetytimer(timer)})
}
}
}
}
func twntytimer(_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeytwenty)
count = timeValue - Int(dateDifference)
labelUpDate(count)
}
func thritytimer (_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeythirty)
count = timeValue - Int(dateDifference)
labelUpDate(count)
}
func sixtytimer(_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeysixty)
count = timeValue - Int(dateDifference)
labelUpDate(count)
}
func ninetytimer(_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeyninety)
count = timeValue - Int(dateDifference)
labelUpDate(count)
}
func labelUpDate (_ minutes : Int) {
let s = minutes % 60
let m = (minutes - s ) / 60
countString = String(format: "%02d : %02d", m, s) //ここにNextViewControllerの executionTimerのラベルを変更する処理を加えたいができない。。。
}
}
例えばNextViewController
の 20のボタンを押しstartのボタンを押したら20分のカウントダウンが始まる処理を書きたいのですがなかなかうまくいきません
編集後↓↓
import UIKit
func secondsToTimerLabel(_ seconds : Int) -> String {
let min = seconds / 60
let sec = seconds % 60
return String(format: "%02d : %02d", min, sec)
}
class NextViewController: UIViewController {
@IBOutlet weak var countdwonTimer: UILabel!
@IBOutlet weak var executionTimer: UILabel!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var kyannseruButton: UIButton!
@IBOutlet weak var twentyButton: UIButton!
@IBOutlet weak var thirtyButton: UIButton!
@IBOutlet weak var sixtyButton: UIButton!
@IBOutlet weak var ninetyButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
stopButton.layer.cornerRadius = 30.0
kyannseruButton.layer.cornerRadius = 30.0
twentyButton.layer.cornerRadius = 22.5
thirtyButton.layer.cornerRadius = 22.5
sixtyButton.layer.cornerRadius = 22.5
ninetyButton.layer.cornerRadius = 22.5
self.executionTimer.text = secondsToTimerLabel(TimerManager.shared.count)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
TimerManager.shared.onCountDidSet = { count in
self.executionTimer.text = secondsToTimerLabel(Int(count))
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
TimerManager.shared.onCountDidSet = nil
}
@IBAction func stopButtonAction(_ sender: UIButton) {
if TimerManager.shared.isRunning {
if TimerManager.shared.timer?.isValid != true { //######ここと
TimerManager.shared.startCount()
stopButton.setTitle("stop", for: .normal)
twentyButton.isUserInteractionEnabled = false
thirtyButton.isUserInteractionEnabled = false
sixtyButton.isUserInteractionEnabled = false
ninetyButton.isUserInteractionEnabled = false
} else {
TimerManager.shared.stop()
stopButton.setTitle("start", for: .normal)
twentyButton.isUserInteractionEnabled = true
thirtyButton.isUserInteractionEnabled = true
sixtyButton.isUserInteractionEnabled = true
ninetyButton.isUserInteractionEnabled = true
} //#############################3
} else {
TimerManager.shared.startCount()
stopButton.setTitle("stop", for: .normal)
twentyButton.isUserInteractionEnabled = false
thirtyButton.isUserInteractionEnabled = false
sixtyButton.isUserInteractionEnabled = false
ninetyButton.isUserInteractionEnabled = false
}
}
@IBAction func cancelButtonAction(_ sender: UIButton) {
TimerManager.shared.cancel()
twentyButton.isUserInteractionEnabled = true
thirtyButton.isUserInteractionEnabled = true
sixtyButton.isUserInteractionEnabled = true
ninetyButton.isUserInteractionEnabled = true
TimerManager.shared.count = 0
self.executionTimer.text = secondsToTimerLabel(0)
stopButton.setTitle("start", for: .normal)
}
@IBAction func twentyButtonAction(_ sender: UIButton) {
self.executionTimer.text = secondsToTimerLabel(1200)
TimerManager.shared.nowNumber = 20
}
@IBAction func thertyButtonActiojn(_ sender: UIButton) {
self.executionTimer.text = secondsToTimerLabel(1800)
TimerManager.shared.nowNumber = 30
}
@IBAction func sixtyButtonAction(_ sender: UIButton) {
self.executionTimer.text = secondsToTimerLabel(3600)
TimerManager.shared.nowNumber = 60
}
@IBAction func ninetyButtonAction(_ sender: UIButton) {
self.executionTimer.text = secondsToTimerLabel(5400)
TimerManager.shared.nowNumber = 90
}
}
import UIKit
class TimerManager {
static var shared = TimerManager()
let settingKey = "timer_value"
let settingKeytwenty = "timer_twenty"
let settingKeythirty = "timer_thirty"
let settingKeysixty = "timer_sixty"
let settingKeyninety = "timer_ninety"
var timer : Timer?
var startDate : Date?
var count = 0 {
didSet {
onCountDidSet?(Double(count))
}
}
var onCountDidSet : ((_ count : Double) -> Void)? = nil
var nowNumber : Int = 0
init () {
let settings = UserDefaults.standard
settings.register(defaults: [settingKey : 216000])
let settings20 = UserDefaults.standard
settings20.register(defaults: [settingKeytwenty : 1200])
let settings30 = UserDefaults.standard
settings30.register(defaults: [settingKeythirty : 1800])
let settings60 = UserDefaults.standard
settings60.register(defaults: [settingKeysixty : 3600])
let settings90 = UserDefaults.standard
settings90.register(defaults: [settingKeyninety : 5400])
}
func startCount() {
startDate = Date()
if timer == nil {
switch nowNumber {
case 20 :
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in self.twntytimer(timer)
})
case 30 :
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: {timer in self.thritytimer(timer)})
case 60 :
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
self.sixtytimer(timer)})
case 90 :
timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: { timer in
self.ninetytimer(timer)})
default :
print("Invalid value \(nowNumber)")
}
}
}
func stop() { //###これです
timer?.invalidate()
}
func cancel() {
timer?.invalidate()
timer = nil
}
var isRunning : Bool {
return timer != nil
}
func twntytimer(_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeytwenty)
count = timeValue - Int(dateDifference)
}
func thritytimer (_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeythirty)
count = timeValue - Int(dateDifference)
}
func sixtytimer(_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeysixty)
count = timeValue - Int(dateDifference)
}
func ninetytimer(_ timer : Timer) {
guard let startDate = startDate else {
return
}
let now = Date()
let dateDifference = now.timeIntervalSince(startDate)
let sitting = UserDefaults.standard
let timeValue = sitting.integer(forKey: settingKeyninety)
count = timeValue - Int(dateDifference)
}
}