gpt4 book ai didi

swift - 如何在 View Controller swift 中从扩展中检索值

转载 作者:行者123 更新时间:2023-11-28 06:36:12 24 4
gpt4 key购买 nike

我想从我的 View Controller 的 scrollViewDidScroll 函数中检索值。它在控制台中返回正确的值,这很好。

我的 View Controller :

import UIKit

extension UIPageViewController: UIScrollViewDelegate {
public override func viewDidLoad() {
super.viewDidLoad()
for subView in view.subviews {
if subView is UIScrollView {
(subView as! UIScrollView).delegate = self
}
}
}

public func scrollViewDidScroll(scrollView: UIScrollView) {
let point = scrollView.contentOffset
var percentComplete: CGFloat
percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
print("percentComplete: ", percentComplete)


}

}

class StepsDetailViewController: UIViewController {

var pageIndex: Int!

override func viewDidLoad()
{
super.viewDidLoad()

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

}

View Controller 来自哪里:

import UIKit

class PageControl: UIPageViewController, UIPageViewControllerDataSource {

var pageViewController: UIPageViewController!
var pageTitles: [[String]]!

override func viewDidLoad(){
super.viewDidLoad()

testC().getRecent() { result, error in
self.pageTitles = result;

self.pageViewController = self
self.pageViewController.dataSource = self

let startVC = self.viewControllerAtIndex(0) as StepsDetailViewController

dispatch_async(dispatch_get_main_queue(), {

let viewControllers = NSArray(object: startVC)


self.pageViewController.setViewControllers(viewControllers as? [UIViewController], direction: .Forward, animated: true, completion: nil)

})
}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}



func viewControllerAtIndex(index: Int) -> StepsDetailViewController
{
if ((self.pageTitles.count == 0) || (index >= self.pageTitles.count)) {
return StepsDetailViewController()
}

let vc: StepsDetailViewController = self.storyboard?.instantiateViewControllerWithIdentifier("StepsDetailViewController") as! StepsDetailViewController


vc.pageIndex = index

return vc
}


// MARK: - Page View Controller Data Source

func pageViewController(pageViewController: UIPageViewController, viewControllerBeforeViewController viewController: UIViewController) -> UIViewController?
{

let vc = viewController as! StepsDetailViewController
var index = vc.pageIndex as Int


if (index == 0 || index == NSNotFound)
{
return nil

}

index -= 1
return self.viewControllerAtIndex(index)
}

func pageViewController(pageViewController: UIPageViewController, viewControllerAfterViewController viewController: UIViewController) -> UIViewController? {

let vc = viewController as! StepsDetailViewController
var index = vc.pageIndex as Int

if (index == NSNotFound)
{
return nil
}

index += 1

if (index == self.pageTitles.count)
{
return nil
}

return self.viewControllerAtIndex(index)

}

func presentationCountForPageViewController(pageViewController: UIPageViewController) -> Int
{
return self.pageTitles.count
}

func presentationIndexForPageViewController(pageViewController: UIPageViewController) -> Int
{
return 0
}

}

这可能吗?

最佳答案

你可以这样做:

<强>1。用自定义 UIPageViewController

替换您的扩展
class BasePageViewController: UIPageViewController, UIScrollViewDelegate {

var percentComplete: CGFloat = 0.0 {
didSet { self.percentCompleteDidChange() }
}

func percentCompleteDidChange() {
print("percentCompleteDidChange")
}

public override func viewDidLoad() {
super.viewDidLoad()
for subView in view.subviews {
if subView is UIScrollView {
(subView as! UIScrollView).delegate = self
}
}
}

public func scrollViewDidScroll(scrollView: UIScrollView) {
let point = scrollView.contentOffset
self.percentComplete = fabs(point.x - view.frame.size.width)/view.frame.size.width
print("percentComplete: ", percentComplete)
}
}

<强>2。 PageControl 是 BasePageViewController 的子类

class PageControl: BasePageViewController, UIPageViewControllerDataSource {
// here you have the access to percentComplete of superclass
func printPercentComplete() {
if let percentComplete = self.percentComplete {
print(percentComplete)
}
}

override func percentCompleteDidChange() {
print("percentCompleteDidChange from child")
}
......
}

关于swift - 如何在 View Controller swift 中从扩展中检索值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39043995/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com