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