gpt4 book ai didi

swift - 将功能添加到警报中

转载 作者:行者123 更新时间:2023-11-28 05:58:05 25 4
gpt4 key购买 nike

我正在尝试创建一个警报,该警报会在接收正常时发挥作用。我的应用程序所做的是通过 pickerView 获取产品信息并给出 ID 号。但首先我想确定一下用户提供的产品是否有保障。

这是我的代码:

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 中添加的评论)当按下确定时,我希望这个功能发生:

     func reCheck() {
if let servicesFieldId = servicesField.selectedItem?.id1, let brandsFieldId = brandsField.selectedItem2?.id2 {
label.text = String(servicesFieldId) + String(format: "%02d%02d", brandsFieldId})
}

现在我在 Stackoverflow 上四处搜索,但在类似问题中找不到合适的代码。我如何编写这个指定的 UIAlert?谢谢。

最佳答案

您需要在用户确认消息后调用函数

您可以为 UIViewController 创建警报扩展

extension UIViewController {

func createAlert (title:String, message:String, completion:@escaping (_ result:Bool) -> Void) {
let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.alert)
self.present(alert, animated: true, completion: nil)

alert.addAction(UIAlertAction(title: "Ok", style: .default, handler: { action in
completion(true)
}))

alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: { action in
completion(false)
}))
}
}

在你的 Action 中

@IBAction func reCheckButton(_ sender: Any) {

self.createAlert(title: "title", message: "Mesage") { (isOk) in

if isOk {
if let servicesFieldId = servicesField.selectedItem?.id1, let brandsFieldId = brandsField.selectedItem2?.id2 {
label.text = String(servicesFieldId) + String(format: "%02d%02d", brandsFieldId})
}
}
}

关于swift - 将功能添加到警报中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50763475/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com