[英]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沒有觸發。
從您的解釋中,我得知兩個UIViewControllers沒有以任何方式鏈接。
當您單擊按鈕以觸發@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
}
然后將調用您的函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.