![](/img/trans.png)
[英]setNavigationBarHidden doesn't work after setViewControllers
[英]setViewControllers doesn't work
我正在嘗試使用NEXT按鈕更改我的UIPageViewController的當前ViewController。 所以我使用委托調用我的mainViewController中的containterViewController中的函數。 但它不執行setViewControllers行。
在這里你可以看到我的代碼:
這是我使用我的委托調用的方法:
func forwardPage() {
print("Start")
currentPage += 1
print(vcs[currentPage])
self.setViewControllers([vcs[currentPage]], direction: .forward, animated: true) { (true) in
print("Done")
}
}
這是我的代表:
protocol WalkthroughViewControllerDelegate {
func forwardPage()
func currentIndex() -> Int
}
這是連接到我的NEXT按鈕的功能:
@IBAction func nextButtonTapped(_ sender: Any) {
if let index = delegate?.currentIndex() {
print("Here... \(index)")
switch index {
case 0...1:
print("Forwarding...")
delegate?.forwardPage()
case 2:
dismiss(animated: true, completion: nil)
default: break
}
}
updateUI()
}
除了“完成”之外的所有內容都會被打印出來
我將衷心感謝您的幫助
我一直在努力,因為這已經有一段時間了
非常感謝你 :)
編輯:也許這發生是因為UIPageViewController在containerView中。 但我不確定
第二次編輯:我已經為這個問題創建了一個git-hub存儲庫。 這是鏈接: https : //github.com/LennartPhil/App-Popup-Screen 。 我希望你能理解我不會向你展示我的所有文件。
好的 - 問題是你的代碼在viewDidLoad()
:
let storyboard = UIStoryboard(name: "ExtraViewControllers", bundle: nil)
let walkthroughPageVC = storyboard.instantiateViewController(withIdentifier: "WalkthroughPageVC") as! WalkthroughPageViewController
delegate = walkthroughPageVC
正在創建一個WalkthroughPageViewController
的新實例,一旦viewDidLoad()
退出,它就會超出范圍。 所以它不再存在。
您需要做的是在prepare(for segue:...)
獲取對它的引用prepare(for segue:...)
,並將其設置為您的委托:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? WalkthroughPageViewController {
self.delegate = vc
}
}
我分叉你的GitHub倉庫並將文件添加到一個項目中,所以你可以看到它運行: https : //github.com/DonMag/App-Popup-Screen
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.