【发布时间】:2015-09-24 15:25:19
【问题描述】:
我正在尝试选择并设置一个属于另一个 UIViewController 的实例变量,但是,我不知道如何选择另一个 ViewController 并在不使用 segue 的情况下访问其内容。
每当用户检查任务时,应计算已完成任务的百分比,并应设置另一个视图控制器的实例变量。
我意识到我目前正在实例化一个新的视图控制器,而不是选择故事板上已有的那个。我正在使用位于主视图后面的第三方侧边栏菜单,尽管它确实作为单独的场景/视图控制器存在。应该注意的是,这个侧边栏菜单不使用 segue 显示本身。有什么方法可以选择另一个视图控制器并访问它的实例变量吗?
@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)
}
【问题讨论】:
-
现有的“sideBarScene”是如何创建的?创建它的对象可以保存引用并将其提供给此代码吗?如果不是,请将 NSNotification 视为在不相关对象之间进行通信的一种方式。
-
sideBarScene 只是故事板上的一个 UIViewController,标识符为“sideBarScene”。
-
但是必须在运行的应用程序中初始化对象。
标签: ios swift cocoa-touch uiviewcontroller