Как сделать Alert по таймингу ? Swift
Task: Нажимаем на кнопку , начинается отсчет времени. По истечению 5 , 10 , 15 секунд , должно появиться 3 Алерта. Не выходит сделать Алерт по таймингу. Проблема заключается именно в тайминге , с алертом проблем нет. Заранее большое спасибо за помощь! Код:
import UIKit
public class ViewController: UIViewController {
let firstReminder = Property(message: " ", days: 0, hours: 0, minutes: 0 , seconds: 5)
let secondReminder = Property(message: "Good morning", days: 0, hours: 0, minutes: 0 , seconds: 10)
let thirdReminder = Property(message: "Hi", days: 0, hours: 0, minutes: 0 , seconds: 15)
let alert = UIAlertController(title: "You are playing too much", message: "What about break", preferredStyle: UIAlertController.Style.alert)
@IBOutlet weak var lbl: UILabel!
var time = 0
//timer
var timer = Timer()
@IBAction func start(_ sender: UIButton)
{
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.action), userInfo: nil, repeats: true)
if firstReminder.seconds == 5 {
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: {
(action) in
print("OK")
}))
self.present(alert, animated:true)
print("Success")
}
}
@IBAction func pause(_ sender: AnyObject)
{
timer.invalidate()
}
@IBAction func reset(_ sender: AnyObject)
{
//timer.invalidate()
time = 0
lbl.text = ("0")
}
@objc func action()
{
time += 1
lbl.text = String(time)
}
public override func viewDidLoad()
{
super.viewDidLoad()
}
public override func didReceiveMemoryWarning()
{
> super.didReceiveMemoryWarning()
}
}
>public class Property {
var message: String , days = 0 , hours = 1 , seconds = 0 , minutes: Int
init(message: String, days:Int ,hours:Int , minutes: Int , seconds: Int ) {
self.message = message
self.days = days
self.hours = hours
self.seconds = seconds
self.minutes = minutes
}
}
Ответы (1 шт):
Автор решения: Pavel Lazarev
→ Ссылка
Вы не сделали блок таймера.
Вам следует сделать Timer вида:
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (_) in
}
то есть:
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (_) in
time += 1
lbl.text = String(time)
if timer == 5 {
self.present(alert, animated:true)
print("Success")
}
Тем более по вашей логике приложения вы не меняете firstReminder.seconds чтобы использовать эту переменную.
Что-то типа того будет:
public class ViewController: UIViewController {
let firstReminder = Property(message: " ", days: 0, hours: 0, minutes: 0, seconds: 5)
let secondReminder = Property(message: "Good morning", days: 0, hours: 0, minutes: 0, seconds: 10)
let thirdReminder = Property(message: "Hi", days: 0, hours: 0, minutes: 0, seconds: 15)
let alert = UIAlertController(title: "You are playing too much", message: "What about break", preferredStyle: UIAlertController.Style.alert)
@IBOutlet weak var lbl: UILabel!
var time = 0
//timer
var timer = Timer()
public override func viewDidLoad() {
super.viewDidLoad()
setupAlertController()
}
func setupAlertController() {
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: {
(action) in
print("OK")
}))
}
@IBAction func start(_ sender: UIButton) {
timer = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { (_) in
self.action()
})
}
@IBAction func pause(_ sender: AnyObject) {
timer.invalidate()
}
@IBAction func reset(_ sender: AnyObject) {
time = 0
lbl.text = "0"
}
@objc func action() {
time += 1
lbl.text = String(time)
switch time {
case firstReminder.seconds:
self.present(alert, animated: true)
case secondReminder.seconds:
self.present(alert, animated: true)
case thirdReminder.seconds:
self.present(alert, animated: true)
default:
break
}
}
}
public class Property {
var message: String, days = 0, hours = 1, seconds = 0, minutes: Int
init(message: String, days: Int, hours: Int, minutes: Int, seconds: Int) {
self.message = message
self.days = days
self.hours = hours
self.seconds = seconds
self.minutes = minutes
}
}