如何从单个 UIViewController 调用 UIButton 点击​​上的多个自定义警报视图?

How do I call multiple custom alert views on a UIButton tap from a single UIViewController?

我有一个 ViewControllerA 和两个自定义警报,我在 ViewControllerA 的按钮单击操作上调用第一个自定义警报。

在第一个自定义提醒中,我有一个文本字段和一个按钮。

当我单击第一个自定义警报的按钮操作时,我想打开第二个自定义警报,并且我想在 ViewControllerA 上显示这两个警报。

The expected result is to show both the custom alerts on the single ViewControllerA

请帮我解决这个问题

class ViewControllerA : UIViewController{
@IBAction func ViewControllerAButtonTapped(_ sender: Any) {  
        FirstCustomalert.instance.ShowAlert()
        self.dismiss(animated: true, completion: nil)
    }
}
class FirstCustomAlert : UIView{
 @IBOutlet var parentView: UIView!
    static let instance = FirstCustomAlert()
    @IBOutlet weak var firstTextField: UITextField!
    override init(frame: CGRect) {
        super.init(frame: frame)
        Bundle.main.loadNibNamed("FirstCustomAlert", owner: self, options: nil)

        CommonInit()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func CommonInit(){

        alertView.frame = CGRect(x: center.x, y: center.y, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        ParentView.autoresizingMask = [ .flexibleHeight , .flexibleWidth ]
    }
    func ShowAlert(){
        UIApplication.shared.keyWindow?.addSubview(ParentView)
    }
    @IBAction func SubmitBittonTapped(_ sender: Any) {

        SecondCustomAlert.instances.ShowAlerts()
    }
}
class SecondCustomAlert :  UIView{
 @IBOutlet var parentView: UIView!
    @IBOutlet weak var secondTextField: UITextField!
    static let instances = SecondCustomAlert()
    override init(frame: CGRect) {
        super.init(frame: frame)
 Bundle.main.loadNibNamed("SecondCustomAlert", owner: self, options: nil)
    }
        required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func CommonInit(){
        parentView.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
        parentView.autoresizingMask = [.flexibleWidth , .flexibleHeight ]
    }
    func ShowAlerts(){

        UIApplication.shared.keyWindow?.addSubview(parentView)
    }
}

首先,您需要更改一些内容:

  1. 命名约定:

您的方法应该使用 lowerCamelCase 命名,并且只能 类 并且对象应该是 CamelCase。

  1. 您正在构建要在应用程序的任何位置显示的自定义提醒。由于您已经在使用 .xib,我建议您将警报重构为 ViewController 并使用 present(viewController: animated:)presentationStyle of .overCurrentContext。您可以在您拥有的 keyWindow 上获取 topViewController 并调用 present 函数。然后在您的 AlertViewController 中,您可以制作单独的动画以在单击按钮时显示警报的第二个视图。

您现在要做的是将过多的逻辑放入 UIView,这不是最佳做法,而且可扩展性不强。而且看起来很乱。您正在尝试相对简单地实现一些目标,但当前方法已经创建了 2 个自定义视图,并且您已经遇到回调问题。

添加:

class ViewControllerA : UIViewController{
@IBAction func ViewControllerAButtonTapped(_ sender: Any) {  
        let customAlert = CustomAlertViewController()
        customAlert.modalPresentationStyle = .overCurrentContext
        self.present(customAlert, animated: true, completion: nil)
    }
}