Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 看着我,我明白为什么代表会活着_Ios_Swift_Uiviewcontroller - Fatal编程技术网

Ios 看着我,我明白为什么代表会活着

Ios 看着我,我明白为什么代表会活着,ios,swift,uiviewcontroller,Ios,Swift,Uiviewcontroller,我有以下结构 主视图控制器:负责调用(A)视图控制器。 (A) ViewController:创建一个CustomClass实例,并具有该类的委托。 CustomClass:在每1秒的时间段内,通过委托向(a)视图控制器发送一条消息 直到这里一切都很好。返回主ViewController后,代理将保持活动状态,换句话说,代理将更新(ViewController)变量。我检查了是否调用了(A)ViewController的ViewDidEnglish。 当我再次从主ViewController返回

我有以下结构

主视图控制器:负责调用(A)视图控制器。 (A) ViewController:创建一个CustomClass实例,并具有该类的委托。 CustomClass:在每1秒的时间段内,通过委托向(a)视图控制器发送一条消息

直到这里一切都很好。返回主ViewController后,代理将保持活动状态,换句话说,代理将更新(ViewController)变量。我检查了是否调用了(A)ViewController的ViewDidEnglish。 当我再次从主ViewController返回(A)ViewController时,将创建一个新的变量实例。反正我也不明白

除此之外,我还想理解为什么当我返回主视图控制器时,代理仍然保持活动状态。我正在使用UINavigationItem进行导航

我是IOS开发的初学者

感谢先进

编辑1: Segue从MainViewController调用(A)ViewController。这个片段是通过故事板添加的

MainViewController.swift

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
class ScanDevices : UIViewController, CustomClassDelegate {

    var myInts : [Int] = []

    var customClass : CustomClass!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("viewDidLoad")

        if customClass == nil {
            customClass = CustomClass()
            customClass.customClassDelegate = self
        }
    }

    override func viewWillAppear(animated: Bool) {
        print("viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        print("viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        print("viewWillDisappear")
    }

    override func viewDidDisappear(animated: Bool) {
        print("viewDidDisappear")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func didDiscoverPeripheralInt(peripheral: Int) {

        myInts.append(peripheral)
        print("Number = \(myInts.count)")
    }  
}
class CustomClass : NSObject {

    var customClassDelegate : CustomClassDelegate?

    // MARK: init

    override init() {
        super.init()

        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "writeInt", userInfo: nil, repeats: true)
    }

    func writeInt () {
        CustomClassDelegate?.didDiscoverPeripheralInt(3)
    }
}
AViewController.swift

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
class ScanDevices : UIViewController, CustomClassDelegate {

    var myInts : [Int] = []

    var customClass : CustomClass!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("viewDidLoad")

        if customClass == nil {
            customClass = CustomClass()
            customClass.customClassDelegate = self
        }
    }

    override func viewWillAppear(animated: Bool) {
        print("viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        print("viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        print("viewWillDisappear")
    }

    override func viewDidDisappear(animated: Bool) {
        print("viewDidDisappear")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func didDiscoverPeripheralInt(peripheral: Int) {

        myInts.append(peripheral)
        print("Number = \(myInts.count)")
    }  
}
class CustomClass : NSObject {

    var customClassDelegate : CustomClassDelegate?

    // MARK: init

    override init() {
        super.init()

        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "writeInt", userInfo: nil, repeats: true)
    }

    func writeInt () {
        CustomClassDelegate?.didDiscoverPeripheralInt(3)
    }
}
CustomClass.swift

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
class ScanDevices : UIViewController, CustomClassDelegate {

    var myInts : [Int] = []

    var customClass : CustomClass!

    override func viewDidLoad() {
        super.viewDidLoad()

        print("viewDidLoad")

        if customClass == nil {
            customClass = CustomClass()
            customClass.customClassDelegate = self
        }
    }

    override func viewWillAppear(animated: Bool) {
        print("viewWillAppear")
    }

    override func viewDidAppear(animated: Bool) {
        print("viewDidAppear")
    }

    override func viewWillDisappear(animated: Bool) {
        print("viewWillDisappear")
    }

    override func viewDidDisappear(animated: Bool) {
        print("viewDidDisappear")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func didDiscoverPeripheralInt(peripheral: Int) {

        myInts.append(peripheral)
        print("Number = \(myInts.count)")
    }  
}
class CustomClass : NSObject {

    var customClassDelegate : CustomClassDelegate?

    // MARK: init

    override init() {
        super.init()

        NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "writeInt", userInfo: nil, repeats: true)
    }

    func writeInt () {
        CustomClassDelegate?.didDiscoverPeripheralInt(3)
    }
}

您的
CustomClass
包含对代理的强引用。必须使用
弱标记属性

weak var customClassDelegate : CustomClassDelegate?
您持有对代理的字符串引用。 这是必须的

 weak  var customClassDelegate : CustomClassDelegate?
请参阅以下文档:

您的问题显示了一堆主要是空的方法-它们有何用处?在哪里创建实例?每个班什么时候叫其他班?请提供与问题相关的详细信息。抱歉,我编辑了AViewController代码。CustomClass实例被创建到MainViewController的viewDidLoad方法中。其他空方法仅用于理解视图控制器生命周期。