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

时间:2022-03-02 18:39:38

Iam using UIPageViewController for my app's first open. And third page is my login page. This contains facebook login. When I clicked facebook login button, opening empty page and xcode give me this output "Presenting view controllers on detached view controllers is discouraged < tapusor.LoginPage: 0x101f09a00 >."

我使用UIPageViewController为我的应用程序首次打开。第三页是我的登录页面。这包含facebook登录。当我点击facebook登录按钮时,打开空白页面和xcode给我这个输出“不鼓励在分离的视图控制器上呈现视图控制器 。” :0x101f09a00>

When I dont use UIPageViewController this button is working. So there is my code. How can I fix this issue?

当我不使用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()
}

}

Also I can try this

我也可以试试这个

let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "LoginPage")

to this

对此

let page3: UIViewController! = storyboard.instantiateViewController(withIdentifier: "LoginPage") as! LoginPage

or this

或这个

let page3: LoginPage! = storyboard.instantiateViewController(withIdentifier: "LoginPage") as! LoginPage

Not worked.

没工作。

1 个解决方案

#1


1  

Create delegate for your LoginPage controller with function what will notify about Facebook login button pressed. Something like this:

使用将按下Facebook登录按钮通知的功能为您的LoginPage控制器创建委托。像这样的东西:

protocol LoginPageDelegate: class {
    func loginPageDidPressFacebookButton(_ controller: LoginPage)
}

Now add delegate property to your LoginPage class

现在将delegate属性添加到LoginPage类

class LoginPage: UIViewController {
    weak var delegate: LoginPageDelegate?
}

Now you need to inform delegate when user press Facebook login button

现在,您需要在用户按Facebook登录按钮时通知代表

@IBAction func facebookButtonPressed(button: UIButton) {
    self.delegate?.loginPageDidPressFacebookButton(self)
}

And not you can subscribe to this delegate in your MyPageViewController.

而且您不能在MyPageViewController中订阅此委托。

First you need to make it class conform to LoginPageDelegate

首先,您需要使其类符合LoginPageDelegate

 class MyPageViewController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate, LoginPageDelegate {

    //MARK: LoginPageDelegate methods

    func loginPageDidPressFacebookButton(_ controller: LoginPage) {
        //present what you need here
    }
}

And now before you show you LoginPage:

现在,在向您展示LoginPage之前:

let page3: LoginPage! = storyboard.instantiateViewController(withIdentifier: "LoginPage")
    page3.delegate = self

#1


1  

Create delegate for your LoginPage controller with function what will notify about Facebook login button pressed. Something like this:

使用将按下Facebook登录按钮通知的功能为您的LoginPage控制器创建委托。像这样的东西:

protocol LoginPageDelegate: class {
    func loginPageDidPressFacebookButton(_ controller: LoginPage)
}

Now add delegate property to your LoginPage class

现在将delegate属性添加到LoginPage类

class LoginPage: UIViewController {
    weak var delegate: LoginPageDelegate?
}

Now you need to inform delegate when user press Facebook login button

现在,您需要在用户按Facebook登录按钮时通知代表

@IBAction func facebookButtonPressed(button: UIButton) {
    self.delegate?.loginPageDidPressFacebookButton(self)
}

And not you can subscribe to this delegate in your MyPageViewController.

而且您不能在MyPageViewController中订阅此委托。

First you need to make it class conform to LoginPageDelegate

首先,您需要使其类符合LoginPageDelegate

 class MyPageViewController: UIPageViewController,
UIPageViewControllerDataSource, UIPageViewControllerDelegate, LoginPageDelegate {

    //MARK: LoginPageDelegate methods

    func loginPageDidPressFacebookButton(_ controller: LoginPage) {
        //present what you need here
    }
}

And now before you show you LoginPage:

现在,在向您展示LoginPage之前:

let page3: LoginPage! = storyboard.instantiateViewController(withIdentifier: "LoginPage")
    page3.delegate = self