簡體   English   中英

代表未被調用(instantiateViewControllerWithIdentifier)

[英]Delegate not being called (instantiateViewControllerWithIdentifier)

我的故事板中有5個CalculatorViewController實例,但是只有一個標識符為“ secondOperationViewController”的實例。 他們中的3個使用嵌入的segues連接到我最初的名為ContainerViewController的ViewController。

我希望帶有標識符“ secondOperationViewController”的ViewController從我的ContainerViewController調用方法,但是我無法正確地實例化它,因此未調用委托方法。 這是我的代碼的一部分:

override func viewDidLoad() {
    super.viewDidLoad()
    secondOperationViewController = storyboard.instantiateViewControllerWithIdentifier("secondOperationViewController") as? CalculatorViewController
    secondOperationViewController!.delegate = self
    println(secondOperationViewController)
}

有什么建議嗎?

我也發現了類似的問題:

代理Swift語言

我將CalculatorViewController的實例重寫為:

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        secondOperationViewController = segue!.destinationViewController as? CalculatorViewController
        secondOperationViewController!.delegate = self;
    }

它可以工作,但是我的委托方法是從連接到初始ViewController的所有3個ViewController調用的,因此我需要另一個解決方案。

那么,如何從具有指定標識符的ViewController中完全調用委托方法呢? 有任何想法嗎?


因此,embed segue不適用於“ instantiateViewControllerWithIdentifier”(但適用於模式呈現的segue)。 我不知道為什么。 這是嵌入segue的解決方案:

override func viewDidLoad() {
    super.viewDidLoad()
    let secondOperationViewController:CalculatorViewController = childViewControllers[0] as CalculatorViewController
    secondOperationViewController.delegate = self
}

在情節提要中為您的segue賦予唯一標識符:

在此處輸入圖片說明

然后在設置prepareForSegue之前,在prepareForSegue檢查,以這種方式確切地知道何時設置該代理。

override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
    // Checking to make sure this is the correct segue. This is also protection for the "as?"
    if segue?.identifier == "SecondOVCSeuge" {
        secondOperationViewController = segue!.destinationViewController as? CalculatorViewController
        secondOperationViewController!.delegate = self;
    }       
}

暫無
暫無

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

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