2017-02-27 86 views
0

我試圖執行從ViewController A到ViewController A(相同的VC)segue。我可以做到這一點,當我按Ctrl +從一個按鈕拖動到VC並設置標識符(在我的情況下「相同的VC」)。performSegue從委託方法相同的ViewController

點擊該按鈕時,segue按預期工作。問題是,我需要致電

self.performSegue(withIdentifier: "sameVC", sender: self) 

來自代表。因此,當按鈕被竊聽,我需要調用一個函數,有一個代表,像這樣:

class ViewControllerA: ViewController, MyDelegate { 

    var mC = MyClass() 

    override func viewDidLoad() { 
    mC.delegate = self 
    } 

    @IBAction func buttonTapped(_ sender: UIButton) { 
    mC.myfunc() 
    } 

    func success { 
    // Trying to call delegate 
    self.performSegue(withIdentifies: "sameVC", sender: self) 
    } 
} 

protocol MyDelegate { 
    func success() 
} 

class MyClass { 
    var delegate: MyDelegate? 

    func myfunc() { 
    // ... 
    self.delegate.success() 
    } 
} 

當委託回來ViewControllerA在「成功」功能全,我要執行的SEGUE,但視圖不再存在,因爲segue被稱爲「buttonTapped」功能的隱式。

當segue不會去相同的VC,我只會按Ctrl +從ViewControllerA拖到ViewControllerB,但這是不可能的只有一個ViewController(或我不知道如何做到這一點)。

有什麼辦法可以實現這個目標嗎?

+0

評論人Hassan Eskandari的作品:http://stackoverflow.com/questions/42482380/performsegue-to-same-viewcontroller-from-delegate-method#comment72106011_42482455 – IceCoala

回答

0

來代替Ctrl +從按鈕,按Ctrl +拖動從視圖控制器本身拖動,然後它會等待您致電performSegue

而從去你有一個以上的viewcontrollers你可能想要導航到你可以在故事板中爲您的viewControllers指定一個標識符,並使用storyboard.instantiateViewControllerWithIdentifier方法實例化您所需的ViewController並將其呈現給用戶,例如將它推到當前視圖控制器的頂部。

+0

這是唯一可能的,當我有兩個ViewControllers,你可以不按Ctrl +拖動到相同的VC。 – IceCoala

+0

使用'storyboard.instantiateViewControllerWithIdentifier'來實例化你的視圖控制器,並將它推到當前視圖控制器之上 –