0

I'm trying to select and set an instance variable that is part of another UIViewController, however, I don't know how to select another ViewController and access its contents without using a segue.

Whenever a user checks off a task, a percentage of tasks that are complete should be calculated and another view controller's instance variable should be set.

I realize I'm currently instantiating a new view controller instead of selecting the one I already have on the storyboard. I'm using a third party sidebar menu that resides behind my main view, although it really exists as a separate Scene/ViewController. It should be noted that this sidebar menu doesn't use a segue reveal itself. Is there any method to select another view controller and access it's instance variables?

@IBAction func checkOffTask(sender: UIButton) {

    // Select sidebar view controller
    let sidebarViewController = self.storyboard?.instantiateViewControllerWithIdentifier("sideBarScene") as! SideBarViewController

    // Calculate percentage of completed tasks
        // Select the count of all tasks
        let allTasksCount = Float(firstDataSource.count + secondDataSource.count)

        // Select the count of all completed tasks
        let completedTasksCount = Float(secondDataSource.count)

        // Divide the two to get a percentage
        var completedTaskPercentage = completedTasksCount / allTasksCount

        sidebarViewController.completedTaskPercentageTemporary = String(stringInterpolationSegment: completedTaskPercentage)

    println(sidebarViewController)
    println(sidebarViewController.completedTaskPercentageTemporary)
}
10000RubyPools
  • 1,182
  • 3
  • 11
  • 24
  • How is the existing "sideBarScene" created? Can the object that created it save a reference and provide it to this code? If not, consider NSNotification as a way of communicating between unrelated objects. – Phillip Mills Sep 24 '15 at 15:31
  • sideBarScene is just a UIViewController on the storyboard with an identifier of "sideBarScene". – 10000RubyPools Sep 24 '15 at 15:38
  • But something has to cause the object to be initialized within the running app. – Phillip Mills Sep 24 '15 at 16:13

1 Answers1

0

One trick could be find out current visible view controller and set properties on it. This thread may help you doing this.

Another way could be to get hold to targeted view controller's object when it is intialized by storyboard. For this you would need to rely on prepareForSegue:sender: method. Once you have saved the object you can pass it & use it to set the properties.

Community
  • 1
  • 1
Abhinav
  • 37,684
  • 43
  • 191
  • 309