Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

ios - Swift. After addSubview click listeners dont work

I have a testVC. TestVC hasnt storyboard, this viewController has XIB file. I show this VC when i have no internet. And logic for a show this VC like this:

 let getVC = NoInternetConnectionVC(nibName: "NoInternetConnectionView", bundle: nil)
        if let getWindow = self.window {
            getVC.view.tag = 501
            getVC.view.frame = getWindow.bounds
            getWindow.addSubview(getVC.view)
        }

also i have extension for UIViewController

extension UIViewController {
     var appDelegate: AppDelegate {
     return UIApplication.shared.delegate as! AppDelegate
 }
 
 var sceneDelegate: SceneDelegate? {
     guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
         let delegate = windowScene.delegate as? SceneDelegate else { return nil }
      return delegate
 }
}

extension UIViewController {
 var window: UIWindow? {
     if #available(iOS 13, *) {
         guard let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene,
             let delegate = windowScene.delegate as? SceneDelegate, let window = delegate.window else { return nil }
                return window
     }
     
     guard let delegate = UIApplication.shared.delegate as? AppDelegate, let window = delegate.window else { return nil }
     return window
 }
}

all clicks in the TestVC works if i show this View Controller like this:

navController.pushViewController(NoInternetConnectionVC(nibName: "NoInternetConnectionView", bundle: nil), animated: true)

but it doesn't suit me. I need to show NoInternetConnectionVC like i described above, that is, like this. When i show NoInternetConnectionVC like below all my listeners stop to work

 let getVC = NoInternetConnectionVC(nibName: "NoInternetConnectionView", bundle: nil)
            if let getWindow = self.window {
                getVC.view.tag = 501
                getVC.view.frame = getWindow.bounds
                getWindow.addSubview(getVC.view)
            }

I tried to add line isUserInteractionEnabled to my code, like this

if let getWindow = self.window {
            getVC.view.tag = 501
            getVC.view.isUserInteractionEnabled = true //added line
            getVC.view.frame = getWindow.bounds
            getWindow.addSubview(getVC.view)
        }

but it doesnt work

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have just added it as a subview. After adding subview move your controller to parent.

if let root = UIApplication.shared.windows.first?.rootViewController {
    root.addChild(getVC)
    getVC.didMove(toParent: root)
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...