标签栏内的ViewController没有响应触摸

时间:2017-11-25 03:10:52

标签: ios swift uitabbarcontroller

tapped() func或touchesBegan中的破坏点都没有被触发。但我可以通过将背景更改为绿色来查看视图。

为什么要吃这些东西?

class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

        let tabBarController = UITabBarController()
        let myVC1 = FirstViewController()
        let myVC2 = SecondViewController()

        let nc1 = UINavigationController(rootViewController: myVC1)
        let nc2 = UINavigationController(rootViewController: myVC2)
        let controllers = [nc1, nc2]
        tabBarController.viewControllers = controllers
        window?.rootViewController = tabBarController

        myVC1.tabBarItem = UITabBarItem(
            title: "Local",
            image: nil,
            tag: 1)
        myVC2.tabBarItem = UITabBarItem(
            title: "Online",
            image: nil,
            tag:2)

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.rootViewController = tabBarController
        window?.makeKeyAndVisible()

        return true
    }




class FirstViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Local"
        view.isUserInteractionEnabled = true
        let r = UITapGestureRecognizer(target: self, action: #selector(tapped))
        view.addGestureRecognizer(r)
        view.backgroundColor = .green
    }

    func tapped() {
        let vc = GameViewController()
        navigationController?.pushViewController(vc, animated: true)

    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        tapped()
    }

}

0 个答案:

没有答案