我是swift
和ios
开发的新手。我有两个课程,希望他们连接。我没有使用prepareForSegue
。这就是我所拥有的,某些地方肯定是错的。
protocol TimeDelegate{
func timerDidFinish()
}
class Timer: UIViewController {
// this is where we declare our protocol
var delegate:TimeDelegate?
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 timeFired(sender: UIButton){
delegate?.timerDidFinish()
}
}
import UIKit
class ViewController: UIViewController, TimeDelegate {
var timer:Timer = Timer()
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func timerDidFinish(){
println("Delegate is working")
}
}
出于某种原因, timerDidFinish 未触发。
答案 0 :(得分:5)
根据你的解释,我认为两个UIViewControllers没有任何联系。
当您单击按钮以激活@IBAction func timeFired(sender: UIButton){..}
功能时,您将进入Timer
UIViewController。
然后你什么时候实例化ViewController
?在没有实例化的情况下,委托将永远不会被设置。
如果您只是想调用timerDidFinish()
func并且不想与ViewController
做任何其他事情,请执行以下操作:
class Timer: UIViewController {
var delegate:TimeDelegate?
override func viewDidLoad() {
super.viewDidLoad()
var vc = ViewController()
self.delegate = vc
}
然后你的函数将被调用。