首页 > 解决方案 > 使用委托和协议(Swift)将数据传递回第一个视图控制器的问题

问题描述

所以我要做的是在我关闭它时将一个 String 和一个 Int 从一个 ViewController (NewCellViewController) 传递回前一个 (SecondScreenViewController)。我在 SecondScreenViewController 的方法中添加了一个打印语句,它应该接收此数据,但它没有打印,所以我猜该方法从未运行过。这是我的代码(删除了一些内容以仅包含相关内容):

SecondScreenViewController:

import UIKit

protocol DataDelegate {
    func insertEvent(eventString: String, pos: Int)
}
class SecondScreenViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, DataDelegate {


    var eventNames = ["event1", "event2", "event3"]


    override func viewDidLoad() {
        super.viewDidLoad()
        advance()
    }
    //DataDelegate methods
    func insertEvent(eventString: String, pos: Int)
    {
        print("if this prints, it worked")
        if pos == -1
        {
            eventNames.append(eventString)
        }
        else
        {
            eventNames.insert(eventString, at: pos)
        }
    }

    @objc func advance()
    {
        let vc = NewCellViewController(nibName: "NewCellViewController", bundle: nil)
        vc.delegate = self
        present(vc, animated: true, completion: nil)
    }
}

新CellViewController:

import UIKit

class NewCellViewController: UIViewController {


var delegate:DataDelegate?

@IBOutlet var addEventName: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

}

@IBAction func addItem() {
    insertNewEvent()   
    dismiss(animated: true, completion: nil)
}
func insertNewEvent()
{
    let eventName = addEventName!.text
    delegate?.insertEvent(eventString: eventName!, pos: -1) //add different positions

}

}

标签: iosswiftxcode

解决方案


我用了controller和你一样的名字,只是为了让你更好地理解。

SecondScreenViewController

import UIKit

class SecondScreenViewController: UIViewController {

    var eventNames = ["event1", "event2", "event3"]


    override func viewDidLoad() {
        super.viewDidLoad()

        //advance()
    }

     override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)

        advance()
    }


    private func advance() {
        // Dont forget to add `Storyboard ID` as "NewCellViewController" on your Main.storyboard. 
        // See the image attached below.
        if let vc = self.storyboard?.instantiateViewController(identifier: "NewCellViewController") as? NewCellViewController {
            vc.delegate = self
            present(vc, animated: true)
        }
    }
}

// Better this way.
extension SecondScreenViewController: DataDelegate {
    
    func insertEvent(eventString: String, pos: Int) {
        print(eventString, pos)
    }
    
}

故事板 ID

新CellViewController

import UIKit

// Better to create protocols here.
protocol DataDelegate: class {
    func insertEvent(eventString: String, pos: Int)
}

class NewCellViewController: UIViewController {
    
    weak var delegate: DataDelegate?

    @IBOutlet var addEventName: UITextField!
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    
    
    @IBAction func addItem() {
        insertNewEvent()
        dismiss(animated: true)
    }
    
    private func insertNewEvent() {
        delegate?.insertEvent(eventString: "your text", pos: -1)
        
    }
}

希望这可以帮助。


推荐阅读