Swift 3 - UIPageViewController - 不鼓励在分离的视图控制器上显示视图控制器

时间:2017-07-11 14:51:20

标签: swift uiviewcontroller uistoryboard facebook-login uipageviewcontroller

Iam使用 UIPageViewController 为我的应用首次打开。第三页是我的登录页面。这包含 facebook登录。当我点击 facebook登录按钮时,打开空白页面和xcode会给我输出“不鼓励在分离的视图控制器上显示视图控制器< tapusor.LoginPage:0x101f09a00&gt ;.”

我不使用 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 个答案:

答案 0 :(得分:1)

为您的delegate控制器创建LoginPage,其中包含按下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)
}

并非您可以在delegate订阅此MyPageViewController

首先,您需要使其符合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