简体   繁体   English

如何在不使用Segue的情况下在单独的UIViewController中选择实例变量

[英]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.

相关问题 如何在不使用 Storyboard segue 的情况下将项目从单独的 UIViewController 添加到 UICollectionViewController? - How to add item to a UICollectionViewController from a separate UIViewController without using Storyboard segue? 如何使用情节提要segue将UIViewController连接到UIView,而无需任何代码? - How to connect UIViewController to UIView using storyboard segue, without any code? 从一个UIViewController转到另一个不使用导航(segue)iOS 6 - go from one UIViewController to another without using navigation (segue) iOS 6 如何以编程方式从一个UIViewController切换到另一个,而无需模态或Push? - How to programmatic segue from one UIViewController to another without Modal or Push? ios - 如何在iOS Swift 3中以编程方式推送和呈现给UIViewController而无需segue - How to push and present to UIViewController programmatically without segue in iOS Swift 3 使用Custom Segue弹出UIViewController? - Pop UIViewController using Custom Segue? iOS:访问相同的UIViewController实例(称为Segue) - iOS: Accessing the same instance of UIViewController that called the Segue 如何使用Xcode根据UItableViewCell中的手势动作触发UIViewController segue - How to trigger a UIViewController segue based on gesture actions in a UItableViewCell using Xcode 在不初始化新对象的情况下将其隔离到UIViewController - Segue to UIViewController without initializing a new object 在另一个UIViewController中更新UITableView而无需在Swift中进行设置 - Updating UITableView in another UIViewController without a segue in Swift
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM