从其他控制器

时间:2016-08-20 11:07:13

标签: swift xcode uiviewcontroller uipageviewcontroller

我有这个视图控制器的功能:updateProgressComplete。但我无法将值添加到此函数所在的视图控制器中。

我的视图控制器:

import UIKit

class StepsDetailViewController: UIViewController {

    var pageIndex: Int! = 0
    var titleText: String!
    var stepData: String!
    var pageControl: PageControl?
    let screenSize: CGRect = UIScreen.mainScreen().bounds

    @IBOutlet weak var dateLabel: UILabel!
    @IBOutlet weak var stepsLabel: UILabel!

    var g : ShapeView?

    override func viewDidLoad(){
        super.viewDidLoad()

        self.dateLabel.text = self.titleText
        self.stepsLabel.text = self.stepData

        g = ShapeView(origin: CGPoint(x: UIScreen.mainScreen().bounds.width / 2, y: UIScreen.mainScreen().bounds.height / 2))

        self.view.addSubview(g!)
    }

    func updateProgressComplete(newValue : CGFloat, view: StepsDetailViewController) {
        self.dateLabel.text = "hello" <-- this doesn't work
    }
}

我认为这是行不通的,因为updateProgressComplete来自另一个类:

导入UIKit

class PageControl: BasePageViewController, UIPageViewControllerDataSource {

    var pageViewController: UIPageViewController!
    var pageTitles: [[String]]!
    var complete : CGFloat = 0.0;
    var count = 0

    override func viewDidLoad(){
        super.viewDidLoad()

        HealthKit().recentSteps() { steps, error in
            self.pageTitles = steps;

            self.count = self.pageTitles.count
            self.count -= 1

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

            let startVC = self.viewControllerAtIndex(self.count) as StepsDetailViewController
            let viewControllers = NSArray(object: startVC)
print(viewControllers)
            dispatch_async(dispatch_get_main_queue(), {

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


            })

        }

    }



    override func percentCompleteDidChange() {
        if let percentComplete = self.percentComplete {
            complete = percentComplete

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

            vc.updateProgressComplete(percentComplete, view: vc)
        }
    }

    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.titleText = self.pageTitles[index][0]
            vc.stepData = self.pageTitles[index][1]
            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)

    }
}

我该如何解决这个问题?我认为我有一些事情可以做到这一点:instantiateViewControllerWithIdentifier? updateProgressComplete的值一直在变化,这就是我使用该函数并且不将其作为变量传递的原因。

1 个答案:

答案 0 :(得分:0)

你是对的,instantiateViewControllerWithIdentifier是个问题。如果创建一个新对象。 (&#39; new&#39;与您要更新的内容不同。)但是,您创建PageControl,尝试为其定义委托并将StepsDetailViewController设置为委托,以便您有正确的更新对象。