重新呈现 ViewController 不会重新初始化它

Presenting ViewController anew does not re-initialize it

我有一个 viewController (videocallVC),每次进入 view/loads 时我都想初始化它。 目前,videocallVC 只初始化第一次。如果我离开 videocallVC,转到另一个 viewController 并返回 videocallVC,它在内存中保留最后一个会话,而不是 "refresh"。

如何确保每次显示 videocallVC 时它都被重新初始化?

import OpenTok

class videocallVC: UIViewController, OTSessionDelegate, OTSubscriberKitDelegate, OTPublisherDelegate {

    @IBOutlet var subscribeView: UIView!
    @IBOutlet var publishView: UIView!

    let apiKey = "xxxxxxx"

    var session : OTSession?
    var publisher : OTPublisher?
    var subscriber : OTSubscriber?
    var connection : OTConnection?


    override func viewDidLoad() {
        super.viewDidLoad()

        session = OTSession(
            apiKey: apiKey,
            sessionId: variableInstance.sessionID,
            delegate: self)
    }

    override func viewWillAppear(animated: Bool) {
        doConnect()
    }

    // MARK: - OpenTok Methods

    /**
    * Asynchronously begins the session connect process. Some time later, we will
    * expect a delegate method to call us back with the results of this action.
    */
    func doConnect() {
        if let session = self.session {
            var maybeError : OTError?
            session.connectWithToken(variableInstance.tokboxToken, error: &maybeError)
            if let error = maybeError {
                showAlert(error.localizedDescription)
            }
        }
    }

    /**
    * Sets up an instance of OTPublisher to use with this session. OTPubilsher
    * binds to the device camera and microphone, and will provide A/V streams
    * to the OpenTok session.
    */
    func doPublish() {
        publisher = OTPublisher(delegate: self)

        var maybeError : OTError?
        session?.publish(publisher, error: &maybeError)

        if let error = maybeError {
            showAlert(error.localizedDescription)
        }

        view.addSubview(publisher!.view)
        publisher!.view.frame = publishView.frame

    }

videocallVC 有更多代码,但我想这足以理解问题。

非常感谢任何帮助!谢谢。

方法viewDidLoad只被调用一次。每次出现视图时都会调用方法 viewWillAppear。因此,如果每次视图出现时您都需要做一些事情,请将其移至该方法(如果 UI 行为有意义,您也可以使用 viewDidAppear)。这可能适用于 session = OTSession( ... ) 代码段。

也有可能在您的代码中对事物进行初始化,然后将其存储在变量中,然后,如果变量不是 nil,则不会进行新的初始化。您可以通过在 viewWillAppear.

中将这些变量设置为 nil 来解决该问题

然而,在不知道所有细节的情况下,这可能是一个可能的解决方案,但仍然是盲目的:-)

您的代表如何存储在 OTSession 和 OTPublisher 中?

他们弱吗?

如果它们不弱,View Controller 不能被取消初始化,因为仍然有对它的引用。也许这会导致您的问题。