繁体   English   中英

页面浏览导航栏标题

[英]Page View Navigation Bar Title

我的代码存在这个问题,因此我的PageViewController具有一个功能,其中该功能基于用户正在查看的视图在导航栏上显示视图控制器的标题。 但是,当我运行该应用程序时,它开始时没有标题,并且该功能仅在用户开始滚动浏览页面时才实现。 我想知道是否有什么办法可以解决? 我使用的函数包括一个switch语句,可以在下面以及在整个类的代码中看到:

func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if (!completed) {
            return
        }

        if let firstViewController = viewControllers?.first,
            let arrayIndex = orderedViewControllers.indexOf(firstViewController) {
            switch arrayIndex {
            case 0:
                self.navigationController!.navigationBar.topItem!.title = "Monday"
                break

            case 1:
                self.navigationController!.navigationBar.topItem!.title = "Tuesday"
                break

            case 2:
                self.navigationController!.navigationBar.topItem!.title = "Wednesday"
                break

            case 3:
                self.navigationController!.navigationBar.topItem!.title = "Thursday"

            case 4:
                self.navigationController!.navigationBar.topItem!.title = "Friday"
                break

            case 5:
                self.navigationController!.navigationBar.topItem!.title = "Saturday"
                break

            case 6:
                self.navigationController!.navigationBar.topItem!.title = "Sunday"
                break

            default:
                self.navigationController!.navigationBar.topItem!.title = "Timetable"


            }
        }
    }

代码:(PageViewController.swift)

import UIKit
import ChameleonFramework

class PageViewController: UIPageViewController,UIPageViewControllerDelegate {

    // created for use in the function which determines the title of the navigationBar
    var arrayIndex: Int = 0
    // reference for later function to determine title of navigationBar
    var pageControl = UIPageControl.self

    // NSUserDefaults
    let defaults = NSUserDefaults.standardUserDefaults()

    // set of viewControllers (based on their storyboard ID)

    private(set) lazy var orderedViewControllers: [UIViewController] = {
        return [self.newDayViewController("monday"),
                self.newDayViewController("tuesday"),
                self.newDayViewController("wednesday"),
                self.newDayViewController("thursday"),
                self.newDayViewController("friday"),
                self.newDayViewController("saturday"),
                self.newDayViewController("sunday")
        ]
    }()


    override func viewDidLoad() {
        super.viewDidLoad()


        self.setNeedsStatusBarAppearanceUpdate()
        // setting the datasource & Delegate of the UIPageViewController
        dataSource = self
        delegate = self



        // set the first viewController for the pageView (monday as it is the first in the set)

        if let firstViewController = orderedViewControllers.first {
            setViewControllers([firstViewController],
                               direction: .Forward,
                               animated: true,
                               completion: nil)


        }

    }





    // function to add view controllers
    // function will only instantiateViewControllers which have a storyboard id containing 'day' e.g Monday, Tuesday, Wednesday

    private func newDayViewController(day: String) -> UIViewController {
        return UIStoryboard(name: "Main", bundle: nil) .
            instantiateViewControllerWithIdentifier("\(day)")
    }




    // set the navigationBar title to the dayViewController's title
    // if the user is looking at orderedViewControllers[1], then the title of that day is "Tuesday"

    func pageViewController(pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
        if (!completed) {
            return
        }

        if let firstViewController = viewControllers?.first,
            let arrayIndex = orderedViewControllers.indexOf(firstViewController) {
            switch arrayIndex {
            case 0:
                self.navigationController!.navigationBar.topItem!.title = "Monday"
                break

            case 1:
                self.navigationController!.navigationBar.topItem!.title = "Tuesday"
                break

            case 2:
                self.navigationController!.navigationBar.topItem!.title = "Wednesday"
                break

            case 3:
                self.navigationController!.navigationBar.topItem!.title = "Thursday"

            case 4:
                self.navigationController!.navigationBar.topItem!.title = "Friday"
                break

            case 5:
                self.navigationController!.navigationBar.topItem!.title = "Saturday"
                break

            case 6:
                self.navigationController!.navigationBar.topItem!.title = "Sunday"
                break

            default:
                self.navigationController!.navigationBar.topItem!.title = "Timetable"


            }
        }
    }
}
// Mandatory functions and setup for the pageViewController to work

extension PageViewController: UIPageViewControllerDataSource {


    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
            return nil
        }
        let previousIndex = viewControllerIndex - 1
        guard previousIndex >= 0 else {
            return orderedViewControllers.last
        }
        guard orderedViewControllers.count > previousIndex else {
            return nil
        }
        return orderedViewControllers[previousIndex]
    }

    func pageViewController(pageViewController: UIPageViewController,
                            viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {
        guard let viewControllerIndex = orderedViewControllers.indexOf(viewController) else {
            return nil
        }
        let nextIndex = viewControllerIndex + 1
        let orderedViewControllersCount = orderedViewControllers.count


        guard orderedViewControllersCount != nextIndex else {
            return orderedViewControllers.first
        }
        guard orderedViewControllersCount > nextIndex else {
            return nil
        }
        return orderedViewControllers[nextIndex]
    }
    // set number of viewControllers to be presented
    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {
        return orderedViewControllers.count
    }
    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {
        guard let firstViewController = viewControllers?.first,
            firstViewControllerIndex = orderedViewControllers.indexOf(firstViewController) else {
                return 0
        }
        return firstViewControllerIndex
    }
}

viewDidLoad()方法中添加

self.navigationItem.title

要么

self.title

使用此代码对您有用

override func viewDidLoad() 
{
    self.title="your title"

要么

 self.navigationItem.title = "YourTitle"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM