将功能添加到警报中

时间:2018-06-08 15:03:50

标签: swift xcode uilabel uialertview func

我正在尝试创建一个警报,当收到OK时会生成一个函数。 我的应用程序所做的是它通过pickerView获取产品信息,并提供ID号。但首先我想确定用户提供的产品是否有保证。

这是我的代码:

else

(阅读我在上面的IBAction中添加的评论) 当按下OK时我想要这个功能发生:

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?"

        }

现在我已经在Stackoverflow上搜索了一下,但是我在类似的问题中找不到合适的代码。 如何编写指定的UIAlert? 感谢。

1 个答案:

答案 0 :(得分:1)

您需要在用户确认消息后调用功能

您可以为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)
        }))
    }
}

在你的行动

@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})
                }
            }
        }