当我单击主页上的一行时,会转到下一页,但不显示上下文

时间:2019-05-05 19:07:17

标签: swift xcode

我正在使用coredata来跟踪密码,用户名和标题。 当我单击所有标题主页上的indexrowpath时,为显示密码而设置的标签没有显示密码。

我还想学习如何将变量或函数从一个文件连接到另一个文件。 我不认为selectedPassword已连接,所以我什至尝试使用homeVC.selectedPassword?.item / username / password,但仍然无法正常工作

PasswordVC

import UIKit

class Password: UIViewController {

    @IBOutlet weak var displayTitle: UILabel!
    @IBOutlet weak var displayUsername: UILabel!
    @IBOutlet weak var displayPassword: UILabel!

    var homeVC = Home()
    var selectedPassword : PasswordCoreData?

    override func viewDidLoad() {
        super.viewDidLoad()

        displayTitle.text = selectedPassword?.item
        displayUsername.text = selectedPassword?.username
        displayPassword.text = selectedPassword?.password
    }

}

从homeVC


var mainframe : [PasswordCoreData] = []
    var selectedPassword : PasswordCoreData?

    override func viewDidLoad() {
        super.viewDidLoad()

    }


    override func viewWillAppear(_ animated: Bool) {
        getPasswords()
    }

    func getPasswords() {
        if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {

            if let coreDataPasswords = try? context.fetch(PasswordCoreData.fetchRequest()) as? [PasswordCoreData] {
                mainframe = coreDataPasswords
                tableView.reloadData()

            }
        }
    }



    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return mainframe.count
    }


    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell", for: indexPath)

        cell.textLabel?.text = mainframe[indexPath.row].item

        return cell
    }

    override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            if let context = (UIApplication.shared.delegate as? AppDelegate)?.persistentContainer.viewContext {

                let item = mainframe[indexPath.row]
                context.delete(item)
                getPasswords()
            }
        }
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "seguePassword", sender: mainframe[indexPath.row].item)
    }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let addVC = segue.destination as? addNewPassword {
            addVC.homeVC = self
        }

        if let passwordVC = segue.destination as? Password {

            if let selectedholder = sender as? PasswordCoreData {
                passwordVC.selectedPassword = selectedholder
                passwordVC.homeVC = self
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

首先,编辑您的PasswordVC:

import UIKit

class Password: UIViewController {

    @IBOutlet weak var displayTitle: UILabel!
    @IBOutlet weak var displayUsername: UILabel!
    @IBOutlet weak var displayPassword: UILabel!

    var selectedPassword : PasswordCoreData!

    override func viewDidLoad() {
        super.viewDidLoad()

        displayTitle.text = selectedPassword?.item
        displayUsername.text = selectedPassword?.username
        displayPassword.text = selectedPassword?.password
    }
}

第二,如果发件人的类型不是“ PasswordCoreData”,则阻止segue触发:

override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    if identifier == "addNewPassword" || sender.isKind(of: PasswordCoreData.self) {
       return true
    }
    return false
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let addVC = segue.destination as? addNewPassword {
        addVC.homeVC = self //I don't understand why you're trying to pass on the viewController to another viewController?
    }

    if let passwordVC = segue.destination as? Password {

        if let selectedHolder = sender as? PasswordCoreData {
            passwordVC.selectedPassword = selectedholder
        }
    }
}

第三,将corect对象作为发送者传递:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "seguePassword", sender: mainframe[indexPath.row]) //not the item, but the whole object
}