使用PageViewController创建引导页
数据源
var headers = ["页面1", "页面2", "页面3"]
var footers = ["这里是页面1,具体内容如下", "这里是页面2,具体内容如下,是一个好人的东莨菪碱", "这里是页面3"]
var imageNames = ["iOS", "swift", "beginner"]
代理方法
遵循UIPageViewControllerDataSource
协议
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
//取得当前控制器index
var index = (viewController as! GuideContentViewController).index
//index - 1
index -= 1
//返回前一个控制器
return getViewController(at: index)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
//取得当前控制器index
var index = (viewController as! GuideContentViewController).index
//index - 1
index += 1
//返回后一个控制器
return getViewController(at: index)
}
获取控制器方法
//注意设置stroyBoard
中contViewController
的storyBoardID
func getViewController(at index: Int) -> GuideContentViewController? {
//判断Index是否在区间范围内
if case 0..<headers.count = index{
//根据storyboard中的控制器id创建viewcontroller
if let contentVC = storyboard?.instantiateViewController(withIdentifier: "GuideContentController") as? GuideContentViewController {
//如果创建成功
//传入值
contentVC.headerStr = headers[index]
contentVC.footerStr = footers[index]
contentVC.imageName = imageNames[index]
contentVC.index = index
return contentVC
}
}
return nil
}
设置起始页
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.white
self.dataSource = self
if let startVC = getViewController(at: 0) {
setViewControllers([startVC], direction: .forward, animated: true, completion: nil)
}
}
内容页
指定变量,控件,以及在页面加载时赋值即可。
@IBOutlet weak var headerLabel: UILabel!
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var footerLabel: UILabel!
var index = 0
var headerStr = ""
var footerStr = ""
var imageName = ""
override func viewDidLoad() {
super.viewDidLoad()
headerLabel.text = headerStr
footerLabel.text = footerStr
imageView.image = UIImage(named: imageName)
// Do any additional setup after loading the view.
}