質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

1回答

3238閲覧

swiftで一時停止も可能なタイマーを作りたい。Value of type 'ViewController2' has no member 'timerCounter' 他エラーについて

madeinrachel

総合スコア12

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2019/05/04 08:44

前提・実現したいこと

swiftでタイマーを作りたいです。

スタート、ストップ、リセットができるものを作ろうとしています。また、測った時間を保存してグラフに表したい

発生している問題・エラーメッセージ

ストップさせるためにif timer != nil{
timer.invalidate()を書いたらタイマーは止まるのですがビルドが中断して落ちてしまいます。
今はおそらく他のエラーも重なりBuild Failedになってしまいます。どうしたらいいでしょうか。

また、このような2つのエラーが出ているのですがどのように対処したらいいですか?
Value of type 'ViewController2' has no member 'timerCounter'
Use of unresolved identifier 'overide

エラーメッセージ Value of type 'ViewController2' has no member 'timerCounter' Use of unresolved identifier 'overide swift import UIKit class ViewController2: UIViewController { weak var timer: Timer! //追加 var startTime = Date() @IBOutlet weak var minute: UILabel! @IBOutlet weak var seconds: UILabel! @IBOutlet weak var miniSeconds: UILabel! override func viewDidLoad() { super.viewDidLoad() } @IBAction func start(_ sender: Any) { if timer != nil{ timer.invalidate() } timer = Timer.scheduledTimer( timeInterval: 0.01, target: self, selector: #selector(self.timerCounter), userInfo: nil, repeats: true) startTime = Date() } @IBAction func reset(_ sender: Any) { if timer != nil{ timer.invalidate() minute.text = "00" seconds.text = "00" miniSeconds.text = "00" } } @IBAction func stoptimer(_ sender: Any) { timer.invalidate() print(timer.isValid) } @IBAction func furoru(_ sender: Any) { func timerCounter() { let currentTime = Date().timeIntervalSince(startTime) let minutelo = (Int)(fmod((currentTime/60), 60)) let second = (Int)(fmod(currentTime, 60)) let msec = (Int)((currentTime - floor(currentTime))*100) let sMinute = String(format:"%02d", minutelo) let sSecond = String(format:"%02d", second) let sMsec = String(format:"%02d", msec) minute.text = sMinute seconds.text = sSecond miniSeconds.text = sMsec } overide; func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } }

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

動くかわかりませんがコンパイルが通るように適当に校正しました。

swift

1import UIKit 2 3class ViewController2: UIViewController { 4 5 weak var timer: Timer! //追加 6 var startTime = Date() 7 8 @IBOutlet weak var minute: UILabel! 9 10 @IBOutlet weak var seconds: UILabel! 11 12 @IBOutlet weak var miniSeconds: UILabel! 13 14 override func viewDidLoad() { 15 super.viewDidLoad() 16 } 17 18 @IBAction func start(_ sender: Any) { 19 20 if timer != nil { 21 timer.invalidate() 22 } 23 timer = Timer.scheduledTimer( 24 timeInterval: 0.01, 25 target: self, 26 selector: #selector(self.timerCounter), 27 userInfo: nil, 28 repeats: true) 29 30 startTime = Date() 31 32 } 33 34 @IBAction func reset(_ sender: Any) { 35 36 if timer != nil{ 37 timer.invalidate() 38 39 minute.text = "00" 40 seconds.text = "00" 41 miniSeconds.text = "00" 42 } 43 44 } 45 46 @IBAction func stoptimer(_ sender: Any) { 47 timer.invalidate() 48 print(timer.isValid) 49 } 50 51 @IBAction func furoru(_ sender: Any) { 52 53 54 } 55 56 @objc func timerCounter() { 57 58 let currentTime = Date().timeIntervalSince(startTime) 59 60 let minutelo = (Int)(fmod((currentTime/60), 60)) 61 62 let second = (Int)(fmod(currentTime, 60)) 63 64 let msec = (Int)((currentTime - floor(currentTime))*100) 65 66 let sMinute = String(format:"%02d", minutelo) 67 let sSecond = String(format:"%02d", second) 68 let sMsec = String(format:"%02d", msec) 69 70 minute.text = sMinute 71 seconds.text = sSecond 72 miniSeconds.text = sMsec 73 74 } 75 76 override func didReceiveMemoryWarning() { 77 super.didReceiveMemoryWarning() 78 } 79}
  • ソースを質問欄に載せる場合は```で囲ってください。
  • timerCounter関数とdidReceiveMemoryWarning関数がfuroru関数の中にいましたので、外に出しました。
  • #selectorで呼び出される関数には@objcをつけます。
  • overide;は誤字なのでoverrideにします。

投稿2019/05/08 07:35

編集2019/05/08 07:36
takabosoft

総合スコア8356

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問