ios - View Controller 委托(delegate)返回 nil

标签 ios swift

我的代码如下。当我按下“完成”或“取消”按钮时,它无法正常工作。我做了一些调试,即使我设置了 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)
    }
}

最佳答案

试试这个,

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)
    }
}

关于ios - View Controller 委托(delegate)返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33072992/

相关文章:

c# - 为 iOS Unity 构建时出现文件路径问题

ios - 将按钮图像设置为 setSelected

android - 使用 Bazel 和 j2objc 在 iOS 和 Android 之间共享代码

objective-c - Xcode Playground 框架导入

ios - 解除 UIAlertController(最佳实践)

iphone - 在完成处理程序中执行选择器不起作用?

iphone - 增量后清除推送通知标志

ios - 快速更改图像不起作用

swift - 使用 PDF 来自 xcasset 的 "Error loading image resource'

Swift:如何使用 Size Classes 更改 UILabel、UIButton 文本的大小?