滑动页面查看控制器并更新页码

时间:2016-10-06 07:08:06

标签: ios swift uilabel uipageviewcontroller

我的程序中有一个UIPageViewController。我已在UITextView的顶部以编程方式添加了UIPageViewController。我想在每次滑动时更新UITextView的文本,以1n的页面2 {1}}或{。}

。{1}}

如何更新n以获得正确的图像编号?我应该在哪里调用TextView函数?

我使用updateStatusLabelText的原因是我无法通过代码将约UITextView添加到UILabel的顶部。

第一堂课如下:

UIPageViewController

第二课

class MainPageViewController: UIPageViewController {

    let dS = MainPageViewControllerDataSource()

    var statusText : String = ""

    var statusTextView = UITextView()

    override func viewDidLoad() {

        super.viewDidLoad()

        statusTextView = UITextView(frame: view.bounds)

        statusTextView.text = statusText

        statusTextView.editable = false

        statusTextView.textAlignment = .Center

        statusTextView.font = UIFont(name: statusTextView.font!.fontName, size: 18)

        statusTextView.textColor = UIColor.blackColor()

        view.backgroundColor = UIColor.whiteColor()

        view.addSubview(statusTextView)

        view.sendSubviewToBack(statusTextView)

    }

    func updateStatusLabelText(arrayKey:Int){

        statusTextView.text = "\(arrayKey + 1) of \(imageArray.count) "

    }

    override func viewWillAppear(animated: Bool) {

        dataSource = dS

        setViewControllers([dS.viewControllerAtIndex(0) as TutorialViewController], direction: .Forward, animated: true, completion: nil)

    }

}

第三课:

class MainPageViewControllerDataSource: NSObject, UIPageViewControllerDataSource,UIPageViewControllerDelegate {

    var tutorialViewControllerDelegate: TutorialViewControllerDelegate?

    var pageIndex = Int()

    func viewControllerAtIndex(index: Int) -> TutorialViewController {

        let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("tutorialVC") as! TutorialViewController

        vc.myImage = imageArray[index]

        vc.pageIndex = index

        self.pageIndex = index

        return vc

    }

    func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController? {

        var index = (viewController as! TutorialViewController).pageIndex

        index -= 1

        switch index {

        case Int.min ... (-1):

            return nil

        case NSNotFound:

            fatalError("NSNotFound. Should crash.")

        case imageArray.count..<Int.max:

            return nil

        default:

            return viewControllerAtIndex(index)

        }

    }



    func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

        var index = (viewController as! TutorialViewController).pageIndex

        index += 1

        switch index {

        case Int.min ... (-1):

            return nil

        case NSNotFound:

            fatalError("NSNotFound. Should crash.")

        case imageArray.count ..< Int.max:

            return nil

        default:

            return viewControllerAtIndex(index)

        }

    }

    func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int {

        return imageArray.count

    }


    func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int {

        return pageIndex - 1

    }

}

0 个答案:

没有答案