我的代码如下。当我按下'完成'或者'取消'按钮,它不会按我的意愿工作。我做了一些调试,delegate
即{I} 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)
}
}
答案 0 :(得分:1)
我无法从您的代码中了解您如何处理EditViewController
中ViewController
的开头。
我猜测您的prepareForSegue:sender:
未被调用,导致代理未设置。要解决此问题,您需要在performSegueWithIdentifier:sender:
处添加self.performSegueWithIdentifier("EditView", sender: self)
在
EditViewController
您应该用该呼叫替换正在执行showViewController:sender:
开放的任何代码。
我有一个应用程序使用Show
从第一个视图控制器打开第二个视图控制器,即使我的故事板中定义了prepareForSegue:sender:
segue。我的故事板中的segue在这种情况下不会被使用,showViewController(myVC, sender: self)
永远不会被调用。
如果我替换
performSegueWithIdentifier("mySegue", sender: self)
与
prepareForSegue:sender:
然后prepareForSegue:sender:
将被调用。如果我在seen
中设置了一个委托,那么委托将在segue发生之前设置。
答案 1 :(得分:1)
试试这个,
class ViewController: UIViewController,EditViewControllerDelegate {
var controller: EditViewController?
@IBOutlet weak var label: UILabel!
//页面跳转时
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "EditView" {
//通过seque的标识获得跳转目标
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)
}
}