gpt4 book ai didi

ios - 如何将警报弹出 View 与自定义警报弹出 View 切换?

转载 作者:行者123 更新时间:2023-11-29 05:29:30 25 4
gpt4 key购买 nike

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

以下是我想要替换基本 AlertView 的自定义 xib Custom Alert

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


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)
}

}

最佳答案

您可以将 View Controller View 添加到 HomeViewController 并为新 View 添加动画,如模态当前 View Controller 。

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 View 。

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)
}


}


关于ios - 如何将警报弹出 View 与自定义警报弹出 View 切换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57788814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com