Я пытаюсь создать оповещение, которое выполняет функцию, когда получено в порядке.что мое приложение делает, так это то, что оно берет информацию о продукте через pickerView и выдает идентификационные номера.но сначала я хочу убедиться, что продукт, предоставленный пользователем, гарантирован.
Вот мой код:
import UIKit
import Foundation
struct Service {
var id1: Int
var title: String
}
struct Brand {
var id2: Int
var title: String
ko}
class PickerTextField: UITextField,UIPickerViewDelegate,UIPickerViewDataSource {
let pickerView = UIPickerView()
var itemList = [Service]()
var selectedItem:Service?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@objc func textEdited(_ sender:PickerTextField)
{
self.text = itemList[pickerView.selectedRow(inComponent: 0)].title
}
override func draw(_ rect: CGRect) {
super.draw(rect)
self.tintColor = UIColor.clear
self.addTarget(self, action: #selector(textEdited(_:)), for: .editingChanged)
pickerView.showsSelectionIndicator = true
pickerView.delegate = self
pickerView.dataSource = self
self.inputView = pickerView
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = .black
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
toolBar.items = [cancelButton, spaceButton, doneButton]
self.inputAccessoryView = toolBar
}
@objc func doneBtnAction(_ sender:UIBarButtonItem) {
resignFirstResponder()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return itemList.count
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let title = itemList[row].title
return NSAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor:UIColor.black])
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.selectedItem = itemList[row]
self.text = itemList[row].title
}
}
class PickerTextField2: UITextField,UIPickerViewDelegate,UIPickerViewDataSource {
let pickerView2 = UIPickerView()
var itemList2 = [Brand]()
var selectedItem2:Brand?
override init(frame: CGRect) {
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
@objc func textEdited(_ sender:PickerTextField)
{
self.text = itemList2[pickerView2.selectedRow(inComponent: 0)].title
}
override func draw(_ rect: CGRect) {
super.draw(rect)
self.tintColor = UIColor.clear
self.addTarget(self, action: #selector(textEdited(_:)), for: .editingChanged)
pickerView2.showsSelectionIndicator = true
pickerView2.delegate = self
pickerView2.dataSource = self
self.inputView = pickerView2
let toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.default
toolBar.isTranslucent = true
toolBar.tintColor = .black
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
let spaceButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancelButton = UIBarButtonItem(title: "Cancel", style: .plain, target: self, action: #selector(doneBtnAction(_:)))
toolBar.items = [cancelButton, spaceButton, doneButton]
self.inputAccessoryView = toolBar
}
@objc func doneBtnAction(_ sender:UIBarButtonItem) {
resignFirstResponder()
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return itemList2.count
}
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
let title = itemList2[row].title
return NSAttributedString(string: title, attributes: [NSAttributedStringKey.foregroundColor:UIColor.black])
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.selectedItem2 = itemList2[row]
self.text = itemList2[row].title
}
}
class ViewController: UIViewController {
@IBOutlet weak var servicesField: PickerTextField!
@IBOutlet weak var brandsField: PickerTextField2!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
servicesField.itemList = [Service(id1: 1, title: "Mobile Phone")]
brandsField.itemList2 = [Brand(id2: 01, title: "Apple")]
}
@IBAction func reCheckButton(_ sender: Any) {
//When pressed: pop up an alert to make sure it's 'Mobile Phone Apple?"
}
(прочитайте комментарий, который я добавил в IBAction выше) и когдаНажата OK. Я хочу, чтобы эта функция выполнялась:
func reCheck() {
if let servicesFieldId = servicesField.selectedItem?.id1, let brandsFieldId = brandsField.selectedItem2?.id2 {
label.text = String(servicesFieldId) + String(format: "%02d%02d", brandsFieldId})
}
Теперь я искал Stackoverflow, но не смог найти правильный код в подобных вопросах.Как я могу кодировать этот указанный UIAlert?Спасибо.