所以我已经向pageViewController添加了三个子视图控制器,它们都可以正常滚动,我可以在模拟器中看到3个子视图控制器,但由于某些原因,我的子视图控制器中的按钮都没有工作。这是我的pageViewController / rootViewController
的代码导入UIKit
类RootPageViewController:UIPageViewController,UIPageViewControllerDataSource,UIPageViewControllerDelegate {
lazy var viewControllerList:[UIViewController] = {
let sb = UIStoryboard(name: "customKeyboardStoryboard", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "peopleVC") as! KeyboardViewController
let vc2 = sb.instantiateViewController(withIdentifier: "animalsVC") as! animalsViewController
let vc3 = sb.instantiateViewController(withIdentifier: "foodVC") as! KeyboardViewController
self.addChildViewController(vc1)
self.addChildViewController(vc2)
self.addChildViewController(vc3)
vc1.view.translatesAutoresizingMaskIntoConstraints = true
vc2.view.translatesAutoresizingMaskIntoConstraints = true
vc3.view.translatesAutoresizingMaskIntoConstraints = true
vc1.willMove(toParentViewController: nil)
vc2.willMove(toParentViewController: nil)
vc3.willMove(toParentViewController: nil)
// self.view.addSubview(vc1.view)
// self.view.addSubview(vc2.view)
// self.view.addSubview(vc3.view)
vc1.didMove(toParentViewController: self)
vc2.didMove(toParentViewController: self)
vc3.didMove(toParentViewController: self)
return [vc1, vc2, vc3]
}()
override func viewDidLoad() {
super.viewDidLoad()
self.dataSource = self
self.delegate = self
// Do any additional setup after loading the view.
if let firstViewController = viewControllerList.first{
self.setViewControllers([firstViewController], direction: .forward, animated: true, completion: nil)
}
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
let previousIndex = vcIndex - 1
guard previousIndex >= 0 else {return nil}
guard viewControllerList.count > previousIndex else {return nil}
return viewControllerList[previousIndex]
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let vcIndex = viewControllerList.index(of: viewController) else {return nil}
let nextIndex = vcIndex + 1
guard viewControllerList.count != nextIndex else {return nil}
guard viewControllerList.count > nextIndex else {return nil}
return viewControllerList[nextIndex]
}
private func setupPageControl() {
let appearance = UIPageControl.appearance()
appearance.pageIndicatorTintColor = UIColor.gray
appearance.currentPageIndicatorTintColor = UIColor.white
appearance.backgroundColor = UIColor.darkGray
}
func presentationCount(for pageViewController: UIPageViewController) -> Int {
setupPageControl()
return self.viewControllerList.count
}
func presentationIndex(for pageViewController: UIPageViewController) -> Int {
return 0
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
这是我的一个子视图控制器代码 - 我现在只有一个按钮链接到它,试图让按钮工作
导入UIKit
类animalsViewController:UIInputViewController,UICollectionViewDelegate,UICollectionViewDataSource {
@IBOutlet var animalsView: UIView!
@IBOutlet weak var animalsCollectonView: UICollectionView!
var animals = ["",""]
override func viewDidLoad() {
super.viewDidLoad()
animalsCollectonView.delegate = self
animalsCollectonView.dataSource = self
// Do any additional setup after loading the view.
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return animals.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let animalsCell = animalsCollectonView.dequeueReusableCell(withReuseIdentifier: "CustomCell1", for: indexPath) as! CustomCell1
animalsCell.myImage.setTitle(animals[indexPath.row], for: UIControlState.normal)
animalsCell.myImage.tag = indexPath.row
return animalsCell
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func nextKeyboard(_ sender: Any) {
advanceToNextInputMode()
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}