简体   繁体   中英

issue passing data back to first view controller with delegate and protocol (Swift)

So what I'm trying to do is pass a String and an Int back from one ViewController (NewCellViewController) to the previous one (SecondScreenViewController) when I close it. I added a print statement in the method in SecondScreenViewController that is supposed to receive this data, and it didn't print so I guess the method never ran. This is my code (removed some stuff to only include whats relevant):

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

NewCellViewController:

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

}

}

I have used the same controller name as yours, just to make you understand better.

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

NewCellViewController

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

Hope, this helps.

You can try using segue in the first VC to push the Second VC and pop the Second VC and come back to First VC. This might help you work with the delegate. And also you can use the UserDefaults to pass and synchronize such values.

Depending on what exactly you want to pass you could use user defaults. Not recommended by many but I feel for simple data it's really quick and effective

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM