Swift 3 - UIPageViewController - 不鼓励在分离的视图控制器上呈现视图控制器
Posted
技术标签:
【中文标题】Swift 3 - UIPageViewController - 不鼓励在分离的视图控制器上呈现视图控制器【英文标题】:Swift 3 - UIPageViewController - Presenting view controllers on detached view controllers is discouraged 【发布时间】:2017-07-11 14:51:19 【问题描述】:我正在使用 UIPageViewController 来打开我的应用程序。第三页是我的登录页面。这包含 facebook 登录。当我单击 facebook 登录按钮 时,打开空白页面和 xcode 给我这个输出 “不鼓励在分离的视图控制器上显示视图控制器
当 我不使用 UIPageViewController 时,此按钮有效。所以有我的代码。我该如何解决这个问题?
import UIKit
class MyPageViewController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate
var pageContainer: UIPageViewController!
// The pages it contains
var pages = [UIViewController]()
// Track the current index
var currentIndex: Int?
private var pendingIndex: Int?
override func viewDidLoad()
super.viewDidLoad()
// Setup the pages
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let page1: UIViewController! = storyboard.instantiateViewController(withIdentifier: "firstVC")
let page2: UIViewController! = storyboard.instantiateViewController(withIdentifier: "secondVC")
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "LoginPage")
pages.append(page1)
pages.append(page2)
pages.append(page3)
pageContainer = UIPageViewController(transitionStyle: .scroll, navigationOrientation: .horizontal, options: nil)
pageContainer.delegate = self
pageContainer.dataSource = self
pageContainer.setViewControllers([page1], direction: UIPageViewControllerNavigationDirection.forward, animated: false, completion: nil)
// Add it to the view
view.addSubview(pageContainer.view)
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController?
let currentIndex = pages.index(of: viewController)!
if currentIndex == 0
return nil
let previousIndex = abs((currentIndex - 1) % pages.count)
return pages[previousIndex]
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController?
let currentIndex = pages.index(of: viewController)!
if currentIndex == pages.count-1
return nil
let nextIndex = abs((currentIndex + 1) % pages.count)
return pages[nextIndex]
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController])
pendingIndex = pages.index(of: pendingViewControllers.first!)
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
我也可以试试这个
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "LoginPage")
到这里
let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "LoginPage") as! LoginPage
或者这个
let page3: LoginPage! = storyboard.instantiateViewController(withIdentifier: "LoginPage") as! LoginPage
没用。
【问题讨论】:
【参考方案1】:为您的LoginPage
控制器创建delegate
,该控制器具有通知Facebook 登录按钮按下的功能。像这样的:
protocol LoginPageDelegate: class
func loginPageDidPressFacebookButton(_ controller: LoginPage)
现在将 delegate
属性添加到您的 LoginPage
类中
class LoginPage: UIViewController
weak var delegate: LoginPageDelegate?
现在你需要在用户按下Facebook login button
时通知代理
@IBAction func facebookButtonPressed(button: UIButton)
self.delegate?.loginPageDidPressFacebookButton(self)
您不能在MyPageViewController
中订阅此delegate
。
首先你需要使它的类符合LoginPageDelegate
class MyPageViewController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate, LoginPageDelegate
//MARK: LoginPageDelegate methods
func loginPageDidPressFacebookButton(_ controller: LoginPage)
//present what you need here
现在在你给你看之前LoginPage
:
let page3: LoginPage! = storyboard.instantiateViewController(withIdentifier: "LoginPage")
page3.delegate = self
【讨论】:
其实我的 facebook 按钮是自动打开到 web 并且没有任何动作。所以当我尝试你的答案时我会改变它并且它有效!谢谢(:以上是关于Swift 3 - UIPageViewController - 不鼓励在分离的视图控制器上呈现视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
滚动应用:UIPageViewController vs UIScrollView
无法将自动布局约束添加到 UIPageViewController