如何将字典从一个视图控制器类传递到另一个?

时间:2020-07-29 17:35:02

标签: ios swift xcode dictionary

我试图在一个视图控制器中列出用户及其密码的列表,将该信息保存在词典中,然后将该词典发送到另一个视图控制器,该控制器要求用户输入用户名/密码组合来授权日志in。(键是用户名,值是密码)。有什么方法可以将字典从SecondVC发送到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!
    }
    

}

我在那里制作了词典,但是它只会在开始时发送,并且在创建原始帐户后不会更新。 (正在发送的字典为空)

2 个答案:

答案 0 :(得分:1)

使用segue在ViewController内添加此方法

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

答案 1 :(得分:0)

这是使控制器使用其创建的某个对象(在本例中为第二个控制器)中的数据的通用模式。

尝试将其应用于您的情况,如果遇到问题,请告诉我。

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