我想在swift中使用協議/委託,雖然我沒有收到任何錯誤,但似乎我的委託沒有被創建。代表返回零
這裏是我的代碼
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標準,符合協議
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")
}
}
委託回來了零,這就是爲什麼我從來沒有得到來自的println Class2,不知道爲什麼它是零。
我更新了我的代碼,提到了GameDetailsVC,但是當代理的println運行在showOptions函數中時,我仍然得到零 – icekomo 2015-03-14 05:42:10
@icekomo嗨!你解決了這個問題嗎? – Harris 2017-06-04 13:57:21