我的代碼如下。當我按下「完成」或「取消」按鈕時,它不起作用。我做了一些調試,並且delegate
是nil
即使我設置它。請幫忙 - 謝謝。視圖控制器委託返回無
class ViewController: UIViewController,EditViewControllerDelegate {
@IBOutlet weak var label: UILabel!
//頁面跳轉時
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditView" {
//通過seque的標識獲得跳轉目標
let controller = segue.destinationViewController as! EditViewController
//設置代理
controller.delegate = self
//將值傳遞給新頁面
controller.oldInfo = label.text
}
}
func editInfo(controller:EditViewController, newInfo:String){
label.text = newInfo
//關閉編輯頁面
controller.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
func editInfoDidCancer(controller:EditViewController){
//關閉編輯頁面
controller.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)
}
}
import UIKit
protocol EditViewControllerDelegate {
func editInfo(controller:EditViewController, newInfo:String)
func editInfoDidCancer(controller:EditViewController)
}
class EditViewController: UIViewController {
@IBOutlet weak var textField: UITextField!
var delegate:EditViewControllerDelegate?
var oldInfo:String?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
if oldInfo != nil{
textField.text = oldInfo
}
}
@IBAction func done(sender: AnyObject) {
delegate?.editInfo(self, newInfo: textField.text!)
}
@IBAction func cancel(sender: AnyObject) {
delegate?.editInfoDidCancer(self)
}
}
你設置的'prepareForSegue'方法中設置斷點?你確定'代表'是否曾經設置?也許你的segue不叫'EditView',但稍有不同? –
只要注意到你有一個'editInfoDidCancer'方法,大概這應該是'editInfoDidCancel'? – mttrb
謝謝,我試過了,代表沒有設置。我怎麼解決這個問題? –