简体   繁体   中英

How can I pass dictionary from one view controller class to another?SWIFT

I am trying to make a list of users and their passwords in one view controller, save that information in a dictionary, and send that dictionary to another view controller which asks the user to input their username/password combination to authorize the log in. (the key is the username and the value is the password). Is there a way I can send the dictionary from SecondVC to the FirstVC?

First View Controller

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var Username: UITextField!
    
    @IBOutlet weak var Verification: UILabel!
    @IBOutlet weak var Password: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        Username.delegate = self
        Password.delegate = self

    }
    
    var usersDict = [String : String]()
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let des = segue.destination as? AccountViewController {
            des.usersDict = usersDict
        }
    }
    


    @IBAction func Authorization(_ sender: Any) { 
        for ( key , value ) in usersDict{
            let v = key.count
            var start = 0
            if start <= v{
                if Username.text == key{
                    if Password.text == value{
                        Verification.text = "Looks Good"
                    }
                }
                else{
                    start += 1
                }
            }
            else{
                Verification.text = "Yikes"
            }
            
            
        }
        


    }
    
}

Second View Controller
class AccountViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var CreateUsername: UITextField!
    @IBOutlet weak var CreatePassword: UITextField!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        CreateUsername.delegate = self
        CreatePassword.delegate = self

        // Do any additional setup after loading the view.
    }
    
    var usersDict = [ String : String ]()

    @IBAction func MakeANewAccount(_ sender: Any) {
        usersDict[CreateUsername.text!] = CreatePassword.text!
    }
    

}

I have made there dictionary, but it will only send in the beginning and won't update after creating the original account. (dictionary it is sending is empty)

With a segue add this method inside ViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let des = segue.destination as? AccountViewController {
        des.usersDict = yourDicHere
    }
}

Here's a general pattern for making a controller work with data from some object it creates, in this case a second controller.

Try applying it to your situation and let me know if you run into problems.

protocol Processor {
    func process(_ dict: [String : String])
}

class FirstController: UIViewController, Processor {
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let controller = segue.destination as? SecondController {
            controller.delegate = self
        } else {
            print("Unexpected view controller \(segue.destination)")
        }
    }
    
    func process(_ dict: [String : String]) {
    }
}

class SecondController: UIViewController {
    var delegate: Processor?
    
    func someWork() {
        if let processor = delegate {
            processor.process(["Name" : "Pwd"])
        } else {
            print("Delegate not assigned")
        }
    }
}

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