簡體   English   中英

如何在不使用Segue的情況下在單獨的UIViewController中選擇實例變量

[英]How to Select an Instance Variable in a Separate UIViewController Without Using a Segue

我試圖選擇並設置一個實例變量,該實例變量是另一個UIViewController的一部分,但是,我不知道如何選擇另一個ViewController並在不使用segue的情況下訪問其內容。

每當用戶檢查任務時,應計算已完成任務的百分比,並應設置另一個視圖控制器的實例變量。

我意識到我當前正在實例化一個新的視圖控制器,而不是選擇我在情節提要上已經擁有的控制器。 我正在使用位於我的主視圖后面的第三方側邊欄菜單,盡管它確實作為一個單獨的Scene / ViewController存在。 應當注意的是,此側邊欄菜單本身並不使用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)
}

一種技巧是找出當前的可見視圖控制器並在其上設置屬性。 該線程可以幫助您做到這一點。

另一種方法可能是通過情節提要初始化目標視圖控制器的對象。 為此,您將需要依賴prepareForSegue:sender:方法。 保存對象后,可以傳遞它並使用它來設置屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM