ios - 如何将数据从搜索栏传递到 UILabel?

标签 ios swift xcode search label

如何将数据从搜索栏传递到不同 View Controller 中的 UILabel

Passing Data between View Controllers 我在堆栈上尝试了多个问题,但它不起作用,或者模拟器最终崩溃了

class FirstVC: UIViewController, DataEnteredDelegate {

    @IBOutlet weak var label: UILabel!

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showSecondViewController" {
            let secondViewController = segue.destination as! SecondVC
            secondViewController.delegate = self
        }
    }

    func userDidEnterInformation(info: String) {
        label.text = info
    }
}



 // protocol used for sending data back

protocol DataEnteredDelegate: class {
    func userDidEnterInformation(info: String)
}

class SecondVC: UIViewController {

    // making this a weak variable so that it won't create a strong reference cycle
    weak var delegate: DataEnteredDelegate? = nil

    @IBOutlet weak var searchBar: UISearchBar!

    @IBAction func sendTextBackButton(sender: AnyObject) {

        // call this method on whichever class implements our delegate protocol
        delegate?.userDidEnterInformation(info: searchBar.text!)

        // go back to the previous view controller
        _ = self.navigationController?.popViewController(animated: true)
    }
}

最佳答案

我在您的代码中没有发现任何导致应用程序崩溃的可疑内容。同时,对于 View Controller 之间的数据传输,我们可以使用委托(delegate)和 NotificationCenter .下面的代码使用NotificationCenter

let kStringTransfer = "StringTransferNotification"

class FirstViewController: UIViewController, DataEnteredDelegate {

    @IBOutlet weak var textField: UITextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(setString(notify:)), name: NSNotification.Name(rawValue: kStringTransfer), object: nil)
    }

    func getSecondVC() {
        if let second = self.storyboard?.instantiateViewController(withIdentifier: "Second") as? ViewController {
            self.navigationController?.pushViewController(second, animated: true)
        }
    }

    @IBAction func searchBarBtn(_ sender: Any) {
        //go to search view controller
        getSecondVC()
    }

    @objc func setString(notify: Notification) {
        //set search bar string to text field
        textField.text = notify.object as? String
    }

}

class SecondViewController: UIViewController, UISearchBarDelegate {

    @IBOutlet weak var searchBar: UISearchBar!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        searchBar.delegate = self
    }

    @IBAction func goBackBtn(_ sender: Any) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: kStringTransfer), object: searchBar.text)
        self.navigationController?.popViewController(animated: true)
    }
}

关于ios - 如何将数据从搜索栏传递到 UILabel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673031/

相关文章:

ios - 如何使用 xmppframework 在 XMPP 中检索我自己最后发送的消息?

ios - 如何使用 Swift Codable 将复杂的 Json 打印到 CollectionView?

ios - 我尝试向我的应用程序添加 map ,但我看到的只是黑屏......为什么?

Xcode 控制台错误 "Program received signal: "EXC_ARITHMETIC""有除法?

c++ - OpenCV 将视频保存到文件

ios - 使用 NSPersistentCloudKitContainer 处理重复项

ios - UILocalNotification 用户信息中的 Swift 字典

ios - 转换为数组后无法显示数据

swift - 使用 Storyboard 中的 View 将其框架用于以编程方式设置的另一个 View

ios - 我可以在没有 Xcode UI 的情况下构建应用程序并将其分发到苹果商店吗?