在本地通知 willPresent 中调用的 Xcode Swift PopUp 函数不起作用,但是当它被 ViewController 中的按钮调用时,它会起作用

问题描述

您好,我正在制作闹钟应用程序,我有一个 NotificationPublisher.swift 文件,我可以在其中处理所有本地通知(闹钟响起时)。在我的 NotificationPublisher.swift 文件中的 willPresent 函数中,我可以调用我的 alarmSound() 函数,该函数位于我的 ViewController.swift 中并且完美运行。但是,我也试图调用一个 createPopUp() 函数,该函数也在我的 ViewController 中,应该会出现一个弹出窗口(弹出窗口只是另一个视图)。我在 ViewControllerScene 上制作了一个完美运行的按钮,弹出窗口出现但是当我尝试不使用该按钮而只是从 NotificationPublisher 中的 willPresent 函数调用 createPopUp 函数时,它不起作用,我的应用程序因线程 1 错误而停止,和控制台读取。

Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value: 
file Alarm_Clock_App/HomeViewController.swift,line 242

2021-05-26 22:43:00.327296-0400 Alarm Clock App[11858:628860] Fatal error: 
Unexpectedly found nil while implicitly unwrapping an Optional value: 
file Alarm_Clock_App/HomeViewController.swift,line 242

这是我的 ViewController 中的所有相关代码

import UIKit
import UserNotifications
import AVFoundation
var player: AVAudioPlayer!

class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    @IBOutlet var popUpView: UIView!
    @IBOutlet var blurView: UIVisualEffectView!
    
    var alarmsHomeScreen = Defaults.getAlarmObjects()
    private let notificationPublisher = NotificationPublisher()

func createPopUp(){
        print("creating pop up")
        // sets the size of the blur view equal to size of overall view
        blurView.bounds = self.view.bounds
        
        // set width to 90 percent of screen and 40 percent of screen height
        popUpView.bounds = CGRect(x: 0,y: 0,width: self.view.bounds.width * 0.9,height: self.view.bounds.height * 0.4)
        animateIn(desiredView: blurView)
        animateIn(desiredView: popUpView)
    }
    
    // Animate in a specified view
    func animateIn(desiredView: UIView) {
        print("animating it in")
        print(desiredView)
        
        let backgroundView = self.view!

        // attach our desired view to the screen (self.view/backgroundView)
        backgroundView.addSubview(desiredView)

        // Sets the views scaling to be 120%
        desiredView.transform = CGAffineTransform(scaleX: 1.2,y: 1.2)
        desiredView.alpha = 0
        desiredView.center = backgroundView.center

        // animate the effect
        UIView.animate(withDuration: 0.3,animations: {
            print("animating")
            desiredView.transform = CGAffineTransform(scaleX: 1.0,y: 1.0)
            desiredView.alpha = 1
        })
    }

    // animate out a specified view
    func animateOut(desiredView: UIView) {
        // animate the effect
        UIView.animate(withDuration: 0.3,animations: {
            desiredView.transform = CGAffineTransform(scaleX: 1.2,y: 1.2)
            desiredView.alpha = 0
        },completion: { _ in
            desiredView.removeFromSuperview()
        })
    }

NotificationPublisher.swift

func userNotificationCenter(_ center: UNUserNotificationCenter,willPresent notification: UNNotification,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationoptions) -> Void) {
        print("Notification is about to be presented")
        ViewController().alarmSound()
        ViewController().createPopUp()
        completionHandler([.badge,.sound,.alert])
    }

发现这是因为在我的 willPresent 函数中,我正常实例化了我的视图控制器,我没有展示它们,并且没有调用视图控制器生活方式方法。所以我试着像这样实例化它。

func userNotificationCenter(_ center: UNUserNotificationCenter,withCompletionHandler completionHandler: @escaping (UNNotificationPresentationoptions) -> Void) {
        print("Notification is about to be presented")
        ViewController().alarmSound()
        let viewController:ViewController = UIStoryboard(name: "Main",bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController
        viewController.createPopUp()
        completionHandler([.badge,.alert])
    }

代码运行,我通过调用我的 createPopUp 函数的 willPresent 函数输入我的 animateIn。但是弹出窗口没有出现,即使我拥有的运行 createPopUp 函数的按钮仍然可以正常工作。

我还是 swift 的新手,所以任何帮助将不胜感激!如果您需要更多信息或代码,我很乐意提供。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)