I'm trying to use a protocol / delegate in swift, and while I'm not getting any errors it seems that my delegate is not being created.
Here is my code
Class 1
import UIKit
protocol GameViewSliding{
func slideGameView()
}
class GameDetailsViewController: UIViewController {
var delegate:GameViewSliding?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func showOptions(sender: AnyObject) {
println("button pressed")
println(delegate)
delegate?.slideGameView()
}
}
Class 2 that conforms to the protocol
import UIKit
var currentHoleNumber:Int = 0
var parThree = false;
var parFive = false;
class GameViewController: UIViewController, GameViewSliding{
var gameDetailsVC:GameDetailsViewController = GameDetailsViewController()
override func viewDidLoad() {
super.viewDidLoad()
println("inside the game class")
gameDetailsVC.delegate = self
}
func slideGameView(){
println("this is from the root controller")
}
}
The delegate comes back nil, which is why I never get the println from Class2, just not sure why it's nil.
You have to set a reference in your GameViewController
to the class GameDetailsViewController
when it should be presented and set it's delegate
in the following way :
gameDetailsViewController.delegate = self
And with that the delegate it's not nil
, of course that set the reference in the prepareForSegue
or any other method you use to present the other ViewController.