TableViewController

时间:2018-01-17 18:36:05

标签: swift tableview uialertview

警报未显示,但会调用代码。

TableViewController是静态且多个部分。

当我单击保存按钮时,如果我的文本字段为空。

必须显示警告。

它正在通过else阻止,只显示警报。

我方法的完整代码:

 @IBAction func saveButton(_ sender: UIBarButtonItem) {
    let ndx = IndexPath(row:0, section: 0)
    var counter:Int = 0
    let cell = self.tableView.cellForRow(at: ndx) as! TableViewCell
    let locationNameCell = self.tableView.cellForRow(at: IndexPath(row:1, section: 0)) as! TableViewCell
        let shoppingDetails = ShoppingDetails(context:managedContext)
        let storeName =  cell.storeName.text!
        let storeFlag  =  validateShoppingList(storeName: storeName)
        if storeFlag == true {
            print("1111 storeFlag = \(storeFlag)")
            while counter < sectionRowCount {
                let item = IndexPath(row:counter, section: 1)
                let itemcell:TableViewCell = self.tableView.cellForRow(at: item) as! TableViewCell
                let list = shoppingDetails.shoppingToList?.mutableCopy() as! NSMutableSet
                let listDetails = ListDetails(context:managedContext)
                if itemcell.listItem.text! == "" {
                    let alert = UIAlertController(title: "TrackActivity", message: "", preferredStyle: .alert)
                    alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: nil))
                    alert.message = "Please Enter Item(s) Name"
                    self.present(alert, animated: true, completion: nil)
                }
                listDetails.listItem = itemcell.listItem.text!
                listDetails.qty = Int64(itemcell.qtytextfield.text!)!
                listDetails.units = itemcell.unitstextfield.text!
                listDetails.isChecked = false

                list.add(listDetails)
                shoppingDetails.addToShoppingToList(list)
                counter = counter + 1
            }
            shoppingDetails.storeName = storeName
            shoppingDetails.location = locationNameCell.locationName.text!
            shoppingDetails.initialLetter = (cell.storeName.text!).first?.description
            let seqNo:Int = ShoppingDetails.getSeqNo(managedObjectContext: managedContext) + 1
            shoppingDetails.seqNo =  Int32(seqNo)
            coreData.saveContext()
            let vc = storyboard!.instantiateViewController(withIdentifier: "StoreDisplayController") as! StoreDisplayController
            vc.managedContext = managedContext
            vc.coreData = coreData
            vc.storeName = cell.storeName.text!
            self.navigationController?.pushViewController(vc, animated: false)
        } else {

            let alert = UIAlertController(title: "TrackActivity", message: "", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: nil))
            if storeFlag == false {
                alert.message = "Please Enter Store Name"
                self.present(alert, animated: true, completion: nil)
            }
        }

    }

请提供您的意见。

2 个答案:

答案 0 :(得分:1)

如果saveButton操作是在自定义的uitableviewcell中编写的 警报视图不存在

请确保在主视图中写明saveButton

答案 1 :(得分:0)

使用AlertController的推荐方法是

class MYAlertController: UIAlertController {

    var alertWindow: UIWindow?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        alertWindow?.isHidden = true
        alertWindow = nil
    }

    func show(animated: Bool = true) {
        alertWindow = UIWindow(frame: UIScreen.main.bounds)
        alertWindow?.rootViewController = UIViewController()

        if let window = UIApplication.shared.delegate?.window {
            alertWindow?.tintColor = window?.tintColor
        }

        alertWindow?.windowLevel = UIApplication.shared.windows.last!.windowLevel + 1
        alertWindow?.makeKeyAndVisible()
        alertWindow?.rootViewController?.present(self, animated: animated, completion: nil)
    }
}

使用此课程显示提醒

<强>用法

func showAlert(_ title: String?,
                   message: String?,
                   actions: AlertActions,
                   style: UIAlertControllerStyle? = .alert) {
        var preferredStyle = style
        if UIDevice.current.userInterfaceIdiom != .phone {
            preferredStyle = .alert
        }

        let alertController = MYGOAlertController(title: title,
                                                  message: message,
                                                  preferredStyle: preferredStyle!)
        for action in actions {
            alertController.addAction(UIAlertAction(title: action.title,
                                                    style: action.style,
                                                    handler: action.actionHandler))
        }
        alertController.show()
    }