import UIKit
class CustomAlertViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var titleLabel: UILabel!
var messageLabel: UILabel!
var confirmButton: UIButton!
var cancelButton: UIButton!
var tableView: UITableView!
var confirmAction: (() -> Void)?
var cancelAction: (() -> Void)?
var message: String?
var confirmTitle: String?
var cancelTitle: String?
override func viewDidLoad() {
super.viewDidLoad()
// 设置背景颜色和半透明效果
view.backgroundColor = UIColor.black.withAlphaComponent(0.5)
// 创建弹出框视图
let alertView = UIView(frame: CGRect(x: 50, y: 200, width: 300, height: 300))
alertView.backgroundColor = .white
alertView.layer.cornerRadius = 10
// 创建标题标签
titleLabel = UILabel(frame: CGRect(x: 0, y: 20, width: 300, height: 30))
titleLabel.textAlignment = .center
alertView.addSubview(titleLabel)
// 创建消息标签
messageLabel = UILabel(frame: CGRect(x: 20, y: 60, width: 260, height: 80))
messageLabel.textAlignment = .center
messageLabel.numberOfLines = 0
alertView.addSubview(messageLabel)
// 创建表格视图
tableView = UITableView(frame: CGRect(x: 20, y: 150, width: 260, height: 100))
tableView.delegate = self
tableView.dataSource = self
alertView.addSubview(tableView)
// 创建确认按钮
confirmButton = UIButton(frame: CGRect(x: 50, y: 260, width: 100, height: 40))
confirmButton.setTitleColor(.blue, for: .normal)
confirmButton.addTarget(self, action: #selector(confirmButtonTapped), for: .touchUpInside)
alertView.addSubview(confirmButton)
// 创建取消按钮
cancelButton = UIButton(frame: CGRect(x: 150, y: 260, width: 100, height: 40))
cancelButton.setTitleColor(.red, for: .normal)
cancelButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
alertView.addSubview(cancelButton)
// 将弹出框视图添加到当前视图中
view.addSubview(alertView)
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// 设置标题和消息文本
titleLabel.text = "Custom Alert"
messageLabel.text = message ?? ""
// 设置按钮标题
confirmButton.setTitle(confirmTitle ?? "Confirm", for: .normal)
cancelButton.setTitle(cancelTitle ?? "Cancel", for: .normal)
}
@objc func confirmButtonTapped() {
confirmAction?()
dismiss(animated: true, completion: nil)
}
@objc func cancelButtonTapped() {
cancelAction?()
dismiss(animated: true, completion: nil)
}
// MARK: - UITableView DataSource & Delegate Methods
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 5
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = "Item \(indexPath.row + 1)"
return cell
}
}
// 在需要显示弹出框的地方调用以下代码
let customAlertVC = CustomAlertViewController()
customAlertVC.modalPresentationStyle = .overFullScreen
customAlertVC.message = "Are you sure you want to proceed?"
customAlertVC.confirmTitle = "Yes"
customAlertVC.cancelTitle = "No"
customAlertVC.confirmAction = {
// 处理确认按钮点击事件
print("Confirm button tapped")
}
customAlertVC.cancelAction = {
// 处理取消按钮点击事件
print("Cancel button tapped")
}
present(customAlertVC, animated: true, completion: nil)
在这个示例中,将 UITableView
添加到了弹出框中,并实现了 UITableViewDataSource
和 UITableViewDelegate
协议来设置表格视图的数据源和代理。在表格视图中,我们添加了 5 个示例行,每行显示 "Item (indexPath.row + 1)" 的文本。
当调用 CustomAlertViewController
来显示弹出框时,就可看到一个包含表格视图的弹出框。