Swift 没有 prepareForSegue 的委托
Swift Delegate without prepareForSegue
我是 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 没有触发。
根据您的解释,我了解到这两个 UIViewController 没有以任何方式链接。
当您单击按钮触发 @IBAction func timeFired(sender: UIButton){..}
函数时,您将进入 Timer
UIViewController。
那你什么时候实例化ViewController
?如果不实例化它,则永远不会设置委托。
如果您只想调用 timerDidFinish()
函数而不想与 ViewController
做任何其他事情,那么请执行以下操作:
class Timer: UIViewController {
var delegate:TimeDelegate?
override func viewDidLoad() {
super.viewDidLoad()
var vc = ViewController()
self.delegate = vc
}
然后你的函数就会被调用。
我是 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 没有触发。
根据您的解释,我了解到这两个 UIViewController 没有以任何方式链接。
当您单击按钮触发 @IBAction func timeFired(sender: UIButton){..}
函数时,您将进入 Timer
UIViewController。
那你什么时候实例化ViewController
?如果不实例化它,则永远不会设置委托。
如果您只想调用 timerDidFinish()
函数而不想与 ViewController
做任何其他事情,那么请执行以下操作:
class Timer: UIViewController {
var delegate:TimeDelegate?
override func viewDidLoad() {
super.viewDidLoad()
var vc = ViewController()
self.delegate = vc
}
然后你的函数就会被调用。