2017-04-16 98 views
0

我有一個應用程序與四個視圖控制器。 1,2和3之間的導航是好的,但從視圖控制器1你可以選擇2或4(這是我的設置)我有一個從1到4的segue。然後我使用unwind segue來取回。但是當我使用segue回到4時,它實例化了一個新的實例4.segue創建視圖控制器swift的新實例

我的問題:有沒有什麼辦法可以訪問用戶上次看到的視圖控制器的同一個實例。

+0

你可以保持到視圖控制器的引用,並推動它,而不是USI另一個例子ng一個segue,無零你使用的導航控制器,你通常會推一個新的實例,根據需要配置它 – Paulw11

+0

對不起,我是一種新手,不知道你的意思。能否請你詳細解釋一下 – c3pNoah

回答

1

就像什麼@ Paul11在評論中說,你應該保持的UIViewController你試圖推,如果你想在同一實例進行訪問

比方說一個參考

var someViewController = SomeViewController() // << this is at the class level scope 

func someSampleFunc() { 
    // doing this would create a new instance of the `SomeViewController` every time you push 
    self.navigationController?.pushViewController(SomeViewController(), animated: true) 

    // whereas if you use the variable which is at the class level scope the memory instance is kept 
    self.navigationController?.pushViewController(someViewController, animated: true) 
} 

的情況下

class Bro { 
    var name = "Some Name" 

    func sayDude() {   
     // since `name` is a class level you can access him here 

     let dude = "DUUUUUUUDE" // this variable's lifetime only exists inside the `sayDude` function, therefore everytime you call `sayDude()` a new instance of `dude` is created 

     print(name) 
     print(dude) 
    } 

    func doBackflip() { 
     sayDude() // 

     print(name + " does backflip") // since `name` is a class level you can access him here 
    } 
} 
+0

感謝所有幫助 – c3pNoah

+0

@ c3pNoah標記我是正確的答案,並且upvote是否有幫助謝謝 –

相關問題