Ios 检查推送通知权限后,从completionHandler块中的nib文件添加子视图

Ios 检查推送通知权限后,从completionHandler块中的nib文件添加子视图,ios,swift3,push-notification,Ios,Swift3,Push Notification,我想检查设备是否对我的应用程序具有PushNotification权限,然后显示对话框 因此我编写了这个函数: func initPushNotificationDialog() { let center = UNUserNotificationCenter.current() center.getNotificationSettings { (settings) in if(settings.authorizationStatus == .notDetermin

我想检查设备是否对我的应用程序具有PushNotification权限,然后显示对话框

因此我编写了这个函数:

func initPushNotificationDialog() {
    let center = UNUserNotificationCenter.current()
    center.getNotificationSettings { (settings) in
        if(settings.authorizationStatus == .notDetermined) {

            let myView = Bundle.loadView(fromNib: "PushNotificationPermissionView", withType: PushNotificationPermissionView.self)
            myView.layer.cornerRadius = 20.0
            myView.layer.borderColor = UIColor.darkGray.cgColor
            myView.layer.borderWidth = 1.0
            myView.center = self.view.center

            myView.layoutIfNeeded()

            DispatchQueue.main.async {
                self.view.addSubview(myView)
            }


        }
    }
}
该函数在视图控制器的viewDidLoad中调用

这是nib文件的外观:

但问题是,如果我运行应用程序,但没有定义权限,我会将PushNotificationPermissionView设置为空,其中没有文本。几秒钟后,显示空内容的视图突然被内容填满

我不明白为什么会发生这种行为,以及这种行为是如何发生的。我尝试了一些事情,比如在主队列中运行它之类的东西,但是没有任何效果。我想让内容直接填写


如果我不在completionHandler块中创建对话框视图,那么一切都很好。

Completion Handler块几乎肯定是在后台线程上运行的。尝试在主线程上实现视图加载、设置和添加子视图:

DispatchQueue.main.async {  

       let myView = Bundle.loadView(fromNib: "PushNotificationPermissionView", withType: PushNotificationPermissionView.self)
        myView.layer.cornerRadius = 20.0
        myView.layer.borderColor = UIColor.darkGray.cgColor
        myView.layer.borderWidth = 1.0
        myView.center = self.view.center

        myView.layoutIfNeeded()

       self.addSubview(subView: myView, toView: self.view) 
}

它到底有没有用?没有。它显示空内容,等待4秒后内容被填满。它应该被立即填充添加代码。该字段已填充。我已更新帖子。代码由故事板填充,4秒后填充哪些数据?谢谢你的回答,但我也试过了,但它不起作用。仍在等待大约4秒钟,内容已被填满。在重新呈现视图控制器OK的viewDidLoad函数中-不要直接调用它,而是尝试将其放在viewDidLoad-self.performselectorself.initPushNotificationDialog的末尾,使用:nil,afterDelay:0.1Hi我找到了解决方案。我必须在主线程中创建整个视图。现在它直接加载内容。你能更新你的答案吗,这样我才能正确地接受它?因此,在添加到子视图之前,myView的初始化需要在主线程内。