简体   繁体   中英

Delegate loosing View Controller objects swift

All,

I have set up a protocol, and the view controller is the delegate of this protocol, like so :

import Foundation

protocol PayButtonProtocol {
    func enablePayButton()
    func disablePayButton()
}

And the view controller is the delegate :

class ViewController:   UIViewController, PayButtonProtocol

The protocol functions are as follows :

func enablePayButton() {
        println("Button enabled")
        PAYBarButton.enabled = false
    }

    func disablePayButton() {
        PAYBarButton.enabled = false
    }

I set a class and assign the delegate :

class Trigger
{
    var delegate:PayButtonProtocol?

    func EnablePayButton()
    {
        delegate?.enablePayButton()
    }
}

Then I set the trigger to run the function :

let localtrigger = Trigger()
        localtrigger.delegate = ViewController()
        localtrigger.EnablePayButton()

This works and the 'button enabled' is displayed in the console. But the Bar Button (PAYBarButton) is nil and it seems that the view controller has lost its hieracy as I cannot access any of the view controllers objects. The View Controller was built with interface builder. Anyone got any ideas ? Is it

localtrigger.delegate = ViewController()

that rebuilds the viewconotroller and makes the original one not accessible ? If so how do i do this ?

if you are creating the localTrigger object inside your ViewController class you can just do:

let localtrigger = Trigger()
localtrigger.delegate = self // self is an instance of ViewController
localtrigger.EnablePayButton()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM