Я новичек в swift. Написал код для вычисления. Не пойму как получить доступ к элементу popup button, который выбран и использовать его в вычислениях? Вот код:
// GlasgoScaleCalculatorViewController.swift
// MedicalCalc
//
// Created by Andrei Dokuchaev on 30.12.2022.
//
import UIKit
class GlasgoScaleCalculatorViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
setPopUpButton()
}
@IBOutlet weak var selectHumanOutlet: UISegmentedControl!
@IBOutlet weak var otkrivanieGlas: UIButton!
@IBOutlet weak var rechevayaReakciya: UIButton!
@IBOutlet weak var dvigatelnayaReakciya: UIButton!
@IBOutlet weak var resultKomaField: UILabel!
func setPopUpButton() {
let optionClosure = {(action : UIAction) in
print(action.title)}
otkrivanieGlas.menu = UIMenu(children : [
UIAction(title : "Отсутствует", state : .on, handler : optionClosure),
UIAction(title : "Реакция на боль", handler : optionClosure),
UIAction(title : "Реакция на голос", handler : optionClosure),
UIAction(title : "Произвольное", handler : optionClosure)
])
otkrivanieGlas.showsMenuAsPrimaryAction = true
otkrivanieGlas.changesSelectionAsPrimaryAction = true
rechevayaReakciya.menu = UIMenu(children : [
UIAction(title : "Отсутствие речи", state : .on, handler : optionClosure),
UIAction(title : "Нечленораздельные звуки в ответ на заданный вопрос", handler : optionClosure),
UIAction(title : "Словесная окрошка, ответ по смыслу не соответствует вопросу", handler : optionClosure),
UIAction(title : "Больной дезориентирован, спутанная речь", handler : optionClosure),
UIAction(title : "Больной ориентирован, быстрый и правильный ответ на заданный вопрос", handler : optionClosure)
])
rechevayaReakciya.showsMenuAsPrimaryAction = true
rechevayaReakciya.changesSelectionAsPrimaryAction = true
dvigatelnayaReakciya.menu = UIMenu(children : [
UIAction(title : "Отсутствие движений", state : .on, handler : optionClosure),
UIAction(title : "Патологическое разгибание в ответ на болевое раздражение", handler : optionClosure),
UIAction(title : "Патологическое сгибание в ответ на болевое раздражение", handler : optionClosure),
UIAction(title : "Отдёргивание конечности в ответ на болевое раздражение", handler : optionClosure),
UIAction(title : "Целенаправленное движение в ответ на болевое раздражение (отталкивание)", handler : optionClosure),
UIAction(title : "Выполнение движений по команде", handler : optionClosure)
])
dvigatelnayaReakciya.showsMenuAsPrimaryAction = true
dvigatelnayaReakciya.changesSelectionAsPrimaryAction = true
}
func setPopUpButtonChildren() {
let optionClosure = {(action : UIAction) in
print(action.title)}
otkrivanieGlas.menu = UIMenu(children : [
UIAction(title : "Отсутствует", state : .on, handler : optionClosure),
UIAction(title : "Реакция на боль", handler : optionClosure),
UIAction(title : "Реакция на голос", handler : optionClosure),
UIAction(title : "Произвольное", handler : optionClosure)
])
otkrivanieGlas.showsMenuAsPrimaryAction = true
otkrivanieGlas.changesSelectionAsPrimaryAction = true
rechevayaReakciya.menu = UIMenu(children : [
UIAction(title : "Плач и интерактивность отсутствуют", state : .on, handler : optionClosure),
UIAction(title : "Не успокаивается при плаче, беспокоен", handler : optionClosure),
UIAction(title : "При плаче успокаивается, но ненадолго, стонет", handler : optionClosure),
UIAction(title : "Ребёнка при плаче можно успокоить, интерактивность неполноценная", handler : optionClosure),
UIAction(title : "Ребёнок улыбается, ориентируется на звук, следит за объектами, интерактивен", handler : optionClosure)
])
rechevayaReakciya.showsMenuAsPrimaryAction = true
rechevayaReakciya.changesSelectionAsPrimaryAction = true
dvigatelnayaReakciya.menu = UIMenu(children : [
UIAction(title : "Отсутствие движений", state : .on, handler : optionClosure),
UIAction(title : "Патологическое разгибание в ответ на болевое раздражение", handler : optionClosure),
UIAction(title : "Патологическое сгибание в ответ на болевое раздражение", handler : optionClosure),
UIAction(title : "Отдёргивание конечности в ответ на болевое раздражение", handler : optionClosure),
UIAction(title : "Целенаправленное движение в ответ на болевое раздражение (отталкивание)", handler : optionClosure),
UIAction(title : "Выполнение движений по команде", handler : optionClosure)
])
dvigatelnayaReakciya.showsMenuAsPrimaryAction = true
dvigatelnayaReakciya.changesSelectionAsPrimaryAction = true
}
@IBAction func selectHumanAction(_ sender: Any, forEvent event: UIEvent) {
switch selectHumanOutlet.selectedSegmentIndex {
case 0:
setPopUpButton()
case 1 :
setPopUpButtonChildren()
default:
break
}
}
var otkrivanGlasSelected: Double = 0
var rechevayaReakciaSelected: Double = 0
var dvigatelReakciyaSelected: Double = 0
var resultKomaGlasgoSelect: String = ""
@IBAction func button(_ sender: Any) {
// вычисления по нажатию кнопки
}
}
}
Метод selectedSegmentIndex не работает так как отсутствует в UIButton. Помогите, пожалуйта.
sender: Any
и есть тот, на ком событие сработало.