Swift 如何将警报添加到我的";删除所有“;及;“保存”;我的视图控制器中的按钮?

Swift 如何将警报添加到我的";删除所有“;及;“保存”;我的视图控制器中的按钮?,swift,uitableview,swift3,Swift,Uitableview,Swift3,我以编程方式在viewController中添加了按钮,我想在点击按钮时添加一个警报,该按钮将显示警报并删除TableViewCell中所有添加的项目。我该怎么做?还有保存按钮,当点击该按钮时,将显示保存警报按钮。 多谢各位 class IncallPantryCheckViewController { let deleteAllButton: UIButton = { let button = UIButton() button.setTitle("

我以编程方式在viewController中添加了按钮,我想在点击按钮时添加一个警报,该按钮将显示警报并删除TableViewCell中所有添加的项目。我该怎么做?还有保存按钮,当点击该按钮时,将显示保存警报按钮。 多谢各位

class IncallPantryCheckViewController {

let deleteAllButton: UIButton = {
        let button = UIButton()
        button.setTitle("Delete All", for: .normal)
        button.titleLabel!.font = UIFont(name: "HelveticaNeue-Bold", size: 20.0)!
        button.setTitleColor(UIColor.orange, for: UIControlState.normal)
        return button
    }()
  

 override func viewDidLoad() {
        super.viewDidLoad()
        
        inCallTableView.register(UINib(nibName: "PantryCheckInCallTableViewCell", bundle: Bundle.main), forCellReuseIdentifier: "PantryCheckInCallTableViewCell")
        
view.addSubview(deleteAllButton)
view.addSubview(saveButton)

deleteAllButton.translatesAutoresizingMaskIntoConstraints = false
deleteAllButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
deleteAllButton.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 45).isActive = true
        
saveButton.translatesAutoresizingMaskIntoConstraints = false
saveButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -20).isActive = true
saveButton.rightAnchor.constraint(equalTo: self.view.rightAnchor, constant: -45).isActive = true
    



 }
}

我假设您要使用iOS提供的警报:

  • 创建显示删除警报和实际执行删除的函数:

     @objc func tappedDelete() {
         let alertController = UIAlertController(title: "Alert", message: "Are you sure you want to delete?", preferredStyle: .alert)
         alertController.addAction(UIAlertAction(title: "YES", style: .destructive, handler: { _ in
             self.performDelete()
         }))
         alertController.addAction(UIAlertAction(title: "NO", style: .cancel, handler: nil))
    
         // present alert, pick one depending if you're using a navigation controller or not.
         //    self.navigationController?.present(alertController, animated: true, completion: nil)
         //    self.present(alertController, animated: true, completion: nil)
     }
    
     func performDelete() {
         print("Do your delete logic here")
     }
    
  • 将目标操作添加到按钮:

     deleteAllButton.addTarget(self, action: #selector(tappedDelete), for: .touchUpInside)
    

  • 对您的“保存”按钮重复上述操作。

    我假设您要使用iOS提供的警报:

  • 创建显示删除警报和实际执行删除的函数:

     @objc func tappedDelete() {
         let alertController = UIAlertController(title: "Alert", message: "Are you sure you want to delete?", preferredStyle: .alert)
         alertController.addAction(UIAlertAction(title: "YES", style: .destructive, handler: { _ in
             self.performDelete()
         }))
         alertController.addAction(UIAlertAction(title: "NO", style: .cancel, handler: nil))
    
         // present alert, pick one depending if you're using a navigation controller or not.
         //    self.navigationController?.present(alertController, animated: true, completion: nil)
         //    self.present(alertController, animated: true, completion: nil)
     }
    
     func performDelete() {
         print("Do your delete logic here")
     }
    
  • 将目标操作添加到按钮:

     deleteAllButton.addTarget(self, action: #selector(tappedDelete), for: .touchUpInside)
    

  • 对“保存”按钮重复上述操作。

    是否希望弹出警报?基本上是一个弹出窗口,上面有一条消息,如“您确定要删除所有内容吗?”。是否要将警报作为弹出窗口?基本上是一个弹出窗口,上面有一条消息,如“您确定要全部删除吗?”。