如何向我的视图控制器中的 "delete all" 和 "save" 按钮添加警报?
how to add an alert to my "delete all" and "save" button in my view controller?
我已经以编程方式在我的 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)
为您的保存按钮重复上述操作。
我已经以编程方式在我的 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)
为您的保存按钮重复上述操作。