【问题标题】:In PageViewController navigation not working在 PageViewController 导航不起作用
【发布时间】:2019-11-10 02:32:29
【问题描述】:

我有 3 种类型的 VC,第一种 VC,第二种 VC 是 PageViewController,有 3 页(我在这里添加了PageController 到普通的ViewController),第三种是FinalViewController

第一个 VC 有按钮,当我单击它时,它将移动到第二个 VC,即 PageViewController(它有 3 页 [3 ViewConrollers] 工作正常)。

PageViewController 加载第一个 VC 后,当我点击我想完全导航 FinalViewController 时,它具有 view 手势。

这里navigation 不工作。

我的第一个 VC 代码

     override func viewWillAppear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = true
    }

    override func viewWillDisappear(_ animated: Bool) {
        self.navigationController?.navigationBar.isHidden = false
    }

    @IBAction func btn(_ sender: Any) {

        let storyboard = self.storyboard?.instantiateViewController(withIdentifier: "NVC")
        self.navigationController?.pushViewController(storyboard!, animated: true)
    }

My PageViewController code

    import UIKit

    class NewViewController: UIViewController, UIPageViewControllerDataSource, UIPageViewControllerDelegate {

    @IBOutlet weak var pagerController: UIPageControl!
    @IBOutlet weak var pageControllerView: UIView!

    // The pages it contains
    var pages =  [UIViewController]()

    // The UIPageViewController
    var pageContainer: UIPageViewController!
    // Track the current index
    var currentIndex: Int?
    private var pendingIndex: Int?

    override func viewDidLoad() {
        super.viewDidLoad()

        // Setup the pages
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let page1 = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
        let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "SecondViewController")
        let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "ThirdViewController")
        pages.append(page1)
        pages.append(page2)
        pages.append(page3)

        page1.variable = "This is strig..."

        // Create the page container
        pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
        pageContainer.delegate = self
        pageContainer.dataSource = self
        pageContainer.setViewControllers([page1], direction: UIPageViewController.NavigationDirection.forward, animated: false, completion: nil)

        // Add it to the view
        pageControllerView.addSubview(pageContainer.view)

        // Configure our custom pageControl
        view.bringSubviewToFront(pagerController)
        pagerController.numberOfPages = pages.count
        pagerController.currentPage = 0


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    // MARK: - UIPageViewController delegates

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.firstIndex(of:viewController)!
        if currentIndex == 0 {
            return nil
        }
        let previousIndex = abs((currentIndex - 1) % pages.count)
        return pages[previousIndex]
    }

    func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
        let currentIndex = pages.firstIndex(of:viewController)!
        if currentIndex == pages.count-1 {
            return nil
        }
        let nextIndex = abs((currentIndex + 1) % pages.count)
        return pages[nextIndex]
    }


    func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
        pendingIndex = pages.firstIndex(of:pendingViewControllers.first!)
    }

    func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if completed {
            currentIndex = pendingIndex
            if let index = currentIndex {
                pagerController.currentPage = index
            }
        }
    }

    }

My `ViewConroller` code means which is loading in `PageViewController` 

    import UIKit

    class ViewController: UIViewController {

    var variable = String()
    @IBOutlet weak var subView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        //Add gesture to incoming call view
        let incomingCallsViewTap = UITapGestureRecognizer(target: self, action: #selector(incomingCallsViewTapFunction(_:)))
        self.subView!.addGestureRecognizer(incomingCallsViewTap)


    }

    // Tap gestrure selector fuction
    @objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
        let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
        self.navigationController?.pushViewController(clvc, animated: false)
    }



    @IBAction func btn(_ sender: Any) {

        print("ViewController one")

        print(variable)
    }

    }

【问题讨论】:

    标签: ios swift cocoa-touch uinavigationcontroller uipageviewcontroller


    【解决方案1】:

    navigationViewController 为 nil,所以我必须执行以下操作:

    (UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
    
    
    // Tap gestrure selector fuction
    @objc func incomingCallsViewTapFunction(_ sender: UITapGestureRecognizer) {
        let clvc = self.storyboard?.instantiateViewController(withIdentifier: "FVC") as! FinalViewController
    //        self.navigationController?.pushViewController(clvc, animated: false)
    
        (UIApplication.shared.keyWindow?.rootViewController as? UINavigationController)?.pushViewController(clvc, animated: true)
    
    }
    

    【讨论】:

    • 谢谢你实际上手势函数调用,但唯一的事情是导航不起作用。我添加了你的代码 VC self.subView!.addGestureRecognizer(incomingCallsViewTap) subView.isUserInteractionEnabled = true
    • @iOS 如果你可以分享你的项目,我可以看看
    • 你能把你的邮件id发给我吗
    • 请检查您的邮件
    • 收到您的电子邮件。调查一下
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-22
    • 2020-06-23
    • 2019-01-15
    • 2018-04-01
    • 2013-07-12
    相关资源
    最近更新 更多