[英]How to Select an Instance Variable in a Separate UIViewController Without Using a Segue
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. 我试图选择并设置一个实例变量,该实例变量是另一个UIViewController的一部分,但是,我不知道如何选择另一个ViewController并在不使用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. 我正在使用位于我的主视图后面的第三方侧边栏菜单,尽管它确实作为一个单独的Scene / ViewController存在。 It should be noted that this sidebar menu doesn't use a segue reveal itself. 应当注意的是,此侧边栏菜单本身并不使用segue显示。 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)
}
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. 为此,您将需要依赖prepareForSegue:sender:
方法。 Once you have saved the object you can pass it & use it to set the properties. 保存对象后,可以传递它并使用它来设置属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.