为其他控制器的功能添加价值

Posted

技术标签:

【中文标题】为其他控制器的功能添加价值【英文标题】:Add value to function from other controller 【发布时间】:2016-08-20 11:07:12 【问题描述】:

我有这个具有以下功能的视图控制器: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】:

你说得对,instantiateViewControllerWithIdentifier 是个问题。如果创建一个新对象。 (“新”与您要更新的不同。)但是,您创建PageControl,尝试为其定义一个委托并将StepsDetailViewController 设置为委托,以便您拥有要更新的正确对象。

【讨论】:

以上是关于为其他控制器的功能添加价值的主要内容,如果未能解决你的问题,请参考以下文章

如何控制需求

如何添加我的功能并在控制器中正确使用它?

如何在 laravel 中为资源控制器添加自定义函数?

如何从firebase中的变量为地图添加价值?

easyadmin crud 控制器:为相关实体增加价值

UITableView 从其他 Viewcontroller 添加行