Архитектурный шаблон "Мрак в моделях" на нескольких языках и платформах
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4KB

  1. import Combine
  2. import UIKit
  3. // MARK: - Ядро-VC
  4. extension Main {
  5. class Core: UIViewController {
  6. let didSelectButton = PassthroughSubject<Int, Never>()
  7. var buttons = [UIButton]()
  8. var subscriptions = [AnyCancellable]()
  9. override func viewDidLoad() {
  10. super.viewDidLoad()
  11. view.backgroundColor = .white
  12. // Создаём 16 кнопок.
  13. for i in 0..<16 {
  14. let btn = UIButton()
  15. btn.tag = i
  16. view.addSubview(btn)
  17. buttons.append(btn)
  18. }
  19. // Расставляем кнопки в сетке 4x4.
  20. for (id, p) in memoryItemPositions(c: M()).enumerated() {
  21. let btn = buttons[id]
  22. btn.frame =
  23. CGRect(
  24. x: CGFloat(p.x),
  25. y: CGFloat(p.y),
  26. width: CGFloat(memorySide()),
  27. height: CGFloat(memorySide())
  28. )
  29. btn.backgroundColor = .blue
  30. }
  31. // Учитываем нажатия кнопок.
  32. for btn in buttons {
  33. btn.addTarget(self, action: #selector(Main.Core.selectButton), for: .touchUpInside)
  34. }
  35. let groups = memoryGroups(c: M())
  36. // Выводим номер нажатой кнопки.
  37. didSelectButton
  38. .sink { i in
  39. print("ИГР MainP.viewDL didSB id/group: '\(i)'/'\(groups[Float(i)])'")
  40. }
  41. .store(in: &subscriptions)
  42. }
  43. @objc func selectButton(_ btn: UIButton) {
  44. didSelectButton.send(btn.tag)
  45. }
  46. }
  47. }