Swift - 单击按钮更改 UIPageViewController 视图
Posted
技术标签:
【中文标题】Swift - 单击按钮更改 UIPageViewController 视图【英文标题】:Swift - Change UIPageViewController view on button click 【发布时间】:2019-04-25 10:28:45 【问题描述】:我正在使用UIPageViewController
,我的目的是在点击UIButton
时切换其UIViewController
之一的当前视图。我已经用谷歌搜索了我的问题并找到了一些类似的主题,但我无法确定答案。
例如,我的第二个UIViewController
vc2
中的按钮应将视图更改为vc3
。
RootPageViewController
import UIKit
class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource
lazy var viewControllerList:[UIViewController] =
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
let vc2 = sb.instantiateViewController(withIdentifier: "mainView")
let vc3 = sb.instantiateViewController(withIdentifier: "addView")
return [vc1, vc2, vc3]
()
override func viewDidLoad()
super.viewDidLoad()
self.dataSource = self
let secondViewController = viewControllerList[1]
self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
guard let vcIndex = viewControllerList.firstIndex(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.firstIndex(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]
第二个 ViewController 内的按钮
@IBAction func addData(_ sender: Any)
【问题讨论】:
可以设置通知器吗? @RaviPadsala 对不起,我是 Swift 新手。您能否在下面的答案中说明您的答案? 您可以在单击按钮时更改同一类中的页面控制器位置? 【参考方案1】:在RootPageViewController
的viewDidLoad
中,将第二个视图控制器按钮的目标添加到self
,并将addData
方法添加到RootPageViewController
。您可以使用setViewControllers
并在该方法中移动到最后一个视图控制器
override func viewDidLoad()
super.viewDidLoad()
self.dataSource = self
if let secondViewController = viewControllerList[1] as? SecondViewController
self.setViewControllers([secondViewController], direction: .forward, animated: false, completion: nil)
secondViewController.button.action = #selector(addData(_:))
@IBAction func addData(_ sender: Any)
if let thirdViewController = viewControllerList.last
self.setViewControllers([thirdViewController], direction: .forward, animated: false, completion: nil)
或
在 MainViewControlle 中保留 addData 方法并像这样向它添加协议
protocol MainVCDelegate
func buttonPlusTapped()
class MainViewController: UIViewController
@IBOutlet var buttonPlus: UIBarButtonItem!
var delegate: MainVCDelegate?
@IBAction func addData(_ sender: Any)
delegate?.buttonPlusTapped()
在 RootPageViewController 中确认这个委托并添加委托方法
class RootPageViewController: UIPageViewController, UIPageViewControllerDataSource, MainVCDelegate
func buttonPlusTapped()
if let thidViewController = viewControllerList.last
self.setViewControllers([thidViewController], direction: .forward, animated: false, completion: nil)
lazy var viewControllerList:[UIViewController] =
let sb = UIStoryboard(name: "Main", bundle: nil)
let vc1 = sb.instantiateViewController(withIdentifier: "timelineView")
let vc2 = sb.instantiateViewController(withIdentifier: "mainView") as! MainViewController
vc2.delegate = self
let vc3 = sb.instantiateViewController(withIdentifier: "addView")
return [vc1, vc2, vc3]
()
【讨论】:
评论不用于扩展讨论;这个对话是moved to chat。以上是关于Swift - 单击按钮更改 UIPageViewController 视图的主要内容,如果未能解决你的问题,请参考以下文章
Swift:如何在单击提交按钮时更改 UIButton 图像
如何在 swift 中单击按钮时更改 uiimage 的图像?
UITableView:单击按钮时如何动态更改单元格高度?迅速