У меня есть решение ваших проблем, я не знаю, как вы кодируете, чтобы показать свой DialAlert
, но по моему вы можете понять, как его реализовать.
Итак, прежде всего в AppDelegate.swift добавьте следующий код.
class AppDelegate: UIResponder, UIApplicationDelegate {
static var shared = UIApplication.shared.delegate as! AppDelegate
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
return true
}
.....
.....
}
После этого, внутри вашего диалогового оповещения объявите протокол и создайте делегата, кроме этой проверки делегата и вызова метода делегата при нажатии кнопки.
DialAlertVC.swift
protocol DialAlertVCDelegate: class {
func callButtonTapped(_ sender: UIButton)
func cancelButtonTapped(_ sender: UIButton)
}
class DialAlertVC: UIViewController {
@IBOutlet weak var dialAlertView: UIView!
weak var delegate : DialAlertVCDelegate?
class func viewController() -> DialAlertVC {
return UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "DialAlertVC") as! DialAlertVC
}
@IBAction func btnCallTapped(_ sender: UIButton) {
if let selfDelegate = self.delegate {
selfDelegate.callButtonTapped(sender)
}
}
@IBAction func btnCancelTapped(_ sender: UIButton) {
if let selfDelegate = self.delegate {
selfDelegate.cancelButtonTapped(sender)
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
Теперь пришло время показать / скрыть DialoAlertVC
, поэтому внутри ViewController
добавьте следующий код.
ViewController.swift
class ViewController: UIViewController {
@IBOutlet weak var lblButtonTapped: UILabel!
var dialAlertVC : DialAlertVC?
@IBAction func btnShowDialAlert(_ sender: UIButton) {
self.showDialAlert(true)
}
func showDialAlert(_ show: Bool) {
if show {
if self.dialAlertVC != nil {
self.dialAlertVC?.view.removeFromSuperview()
self.dialAlertVC = nil
}
dialAlertVC = DialAlertVC.viewController()
dialAlertVC?.delegate = self
AppDelegate.shared.window?.addSubview(dialAlertVC!.view)
} else {
if self.dialAlertVC != nil {
self.dialAlertVC?.view.removeFromSuperview()
self.dialAlertVC = nil
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension ViewController: DialAlertVCDelegate {
func callButtonTapped(_ sender: UIButton) {
self.showDialAlert(false)
self.lblButtonTapped.text = "Call Button Tapped"
}
//------------------------------------------------------------------------------
func cancelButtonTapped(_ sender: UIButton) {
self.showDialAlert(false)
self.lblButtonTapped.text = "Cancel Button Tapped"
}
}
Смотрите демонстрационный проект здесь: https://gofile.io/?c=P2VKCl
Надеюсь, это поможет вам.