【问题标题】:How do I switch the Alert Pop-Up View with a custom Alert popup View?如何使用自定义警报弹出视图切换警报弹出视图?
【发布时间】:2019-09-04 12:52:00
【问题描述】:

我正在尝试创建自定义 AlertView,一旦应用打开,它就会在主屏幕上弹出。在故事板上使用自定义 XIB 或视图控制器。我已经阅读了 UIKit 以找到一种将基础的警报视图切换到我想要呈现但仍然没有骰子的自定义视图的方法。

这是我要替换基本 AlertView Custom Alert 的自定义 xib

我的代码目前在主屏幕打开时显示 AlertView,我只是不知道如何将基本警报视图切换到自定义视图


import UIKit

class HomeViewController: UIViewController {

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

override func viewDidAppear(_ animated: Bool) {
    createAlert(title: "Are you 21 years of Age or Over?", message: "")
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}
func createAlert (title: String, message: String) {
    let alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.actionSheet)

    // CREATING ON BUTTON
    alert.addAction(UIAlertAction(title: "Yes", style: UIAlertAction.Style.default, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        print("Yes")
    }))

    alert.addAction(UIAlertAction(title: "No", style: UIAlertAction.Style.default, handler: { (action) in
        alert.dismiss(animated: true, completion: nil)
        print("No")
    }))

    self.present(alert, animated: true, completion: nil)
}

}

【问题讨论】:

  • 您到底想定制什么?您是否对 UIAlertController 的总体外观感到满意,但只是想更改背景和文本颜色,或者您想使用与 UIAlertController 不同的东西来完成相同的工作?
  • 我不喜欢 UIAlertViewController 的一般外观,我只想用我自己的自定义视图控制器来切换它,这样我就可以更轻松地将自定义视图设计成我想要的样子
  • 然后您可以通过创建 UIView 的子类并添加文本标签和按钮作为子视图来设计自己的。我看到有人已经在下面发布了有关如何执行类似操作的答案。

标签: ios swift xcode customization alert


【解决方案1】:

您可以将视图控制器视图添加到 HomeViewController 并像模态当前视图控制器一样为新视图设置动画。

class HomeViewController: UIViewController {

    var alert: CustomAlertViewController!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }

    override func viewDidAppear(_ animated: Bool) {
        //Make below code as a seperate method and call from as per your conveience

        alert = CustomAlertViewController(nibName: "CustomAlertViewController", bundle: Bundle.main)
        alert.delegate = self
        //animate view like presenting modal view controller
        var rect = alert.view.frame
        rect.origin.y = rect.height
        alert.view.frame = rect

        self.view.addSubview(alert.view)

        UIView.animate(withDuration: TimeInterval(0.5)) {
            rect.origin.y = 0
            self.alert.view.frame = rect
        }
    }


}

extension HomeViewController: CustomAlertProtocol {
    func dismissAlert(isYes: Bool) {
        //capture yes no answer from isYes variable

        self.alert.view.removeFromSuperview()
    }


}

根据需要在 nib 文件中设计您的 CustomAlertViewController 视图。

protocol CustomAlertProtocol: class {
    func dismissAlert(isYes: Bool)
}

class CustomAlertViewController: UIViewController {

    weak var delegate: CustomAlertProtocol?
    override func viewDidLoad() {
        super.viewDidLoad()


        self.view.backgroundColor = UIColor.clear
    }

    @IBAction func yes(id: Any) {
        delegate?.dismissAlert(isYes: true)
    }

    @IBAction func no(id: Any) {
        delegate?.dismissAlert(isYes: false)
    }


}


【讨论】:

  • 似乎没有用我的主视图控制器中的“alert.delegate = self”出现错误,错误代码为线程 1:致命错误:在隐式展开时意外发现 nil可选值
  • 致命错误是崩溃而不是错误。您应该在运行时遇到致命错误。您能否详细说明导致应用崩溃的操作?
  • 运行该应用程序后,它会在主屏幕上打开,并在大约 5 秒后崩溃,不显示自定义警报视图,并将我带回 HomeVC
  • 从错误日志中可以明显看出编译器在运行时找不到CustomAlertViewController的nib文件。为这个 ViewController 创建 nib 文件并根据您的需要设计您的视图或从dropbox.com/s/f5pvqasj5morapv/…下载它
  • 我测试了它,但我仍然在日志中遇到同样的错误
猜你喜欢
  • 1970-01-01
  • 2012-10-31
  • 1970-01-01
  • 2017-11-16
  • 1970-01-01
  • 1970-01-01
  • 2012-07-06
  • 2011-02-05
  • 2014-07-09
相关资源
最近更新 更多