【问题标题】:How to send data back to previous screen IOS/Swift [duplicate]如何将数据发送回上一个屏幕IOS / Swift [重复]
【发布时间】:2020-05-07 11:04:41
【问题描述】:

我正在 IOS/swift 中创建一个库:

takes a user to a scene --> performs a task --> return to the initial scene that called the first while passing a payload back to the user

我已经想出了如何将用户带回调用它的上一个场景,但我的问题是如何使用下面的代码 sn-p 将有效负载发送回:

func switchToPreviousPage(){
    self.dismiss(animated: true, completion: nil)
}

我如何做到这一点?

【问题讨论】:

标签: ios swift


【解决方案1】:

在您的场景中,您可以使用:

  1. 委托模式
  2. 通知/观察者

让我们逐一讨论:

1.委派:

如果您对 Swift 中的协议有所了解,您可以轻松完成。 首先创建一个包含您要实现的所需功能的协议:

protocol FirstControllerDelegate: AnyObject {
    func sendData(data: String)
}

假设你的 firstPage 是 FirstViewController,它有一个 UILabel,我们必须从我们的 secondPage 指派 SecondViewController 给它分配一个字符串。您的 FirstViewController 的结构可能是这样的:

class FirstViewController: UIViewController {
    @IBOutlet weak var textLabel: UILabel!
    @IBAction func gotoSecondPage() {
        let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    }

}

现在你的 FirstViewController 必须确认这个协议,它将实现 sendData(data: ) 方法:

extension FirstViewController: FirstControllerDelegate {
    func sendData(data: String) {
        textLabel.text = data
    }
}

现在作为 iOS 中协议的一项功能,协议可以作为类型(如 Int、String)工作。因此,只需在您的 SecondViewController 中创建一个 FirstControllerDelegate 类型的变量!

class SecondViewController: UIViewController {
    weak var delegate: FirstControllerDelegate!
    @IBAction func switchToPreviousPage() {
        delegate.sendData(data: "Hello")
        self.dismiss(animated: true, completion: nil)
    }
}

您现在可以使用上面创建的变量调用 sendData(data:) 函数!

最后你要做的就是分配委托:

secondVC.delegate = self

它应该在 gotoSecondPage() 方法中!

2。通知/观察者

有了这个,我们的基本想法是在我们的应用程序内部发送一个通知,它可以被内部的任何地方观察到!

所以我们的 SecondViewController 将发送一个嵌入了我们想要传递的所需数据的通知,FirstViewController 将接收到该通知并从通知中提取数据!

每个通知都有一个特定的名称,这将其与其他通知区分开来。我们必须创建名称:

Notification.Name(rawValue: "com.app.notificationObserver")

现在 FirstViewController 将观察到这个特定的通知:

override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(self.changeLabelText(notifcation:)), name: Notification.Name("com.app.notificationObserver"), object: nil)
    }

我们必须定义 changeLabelText(notification:) 方法:

private func changeLabelTExt(notification: NSNotification) {
    if let dataDict = notification.userInfo as NSDictionary? {
        if let message = dataDict["data"] as? String {
            self.textLabel.text = message
        }
    }
}

最后,SecondViewController 会触发 Notification :

@IBAction func switchToPreviousPage() {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "com.app.notificationObserver"), object: ["data": "hello"])
        self.dismiss(animated: true, completion: nil)
    }

就是这样......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-18
    • 2019-05-10
    • 1970-01-01
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多