Ios 在swift5中关闭页面时如何传输参数?
我目前正在研究Swift5。我有个问题。目前有一个WebView启动和一个自定义通知窗口来检查数据。检查数据后,我将关闭通知窗口 当我关闭此处的通知窗口时,如何将数据转发到WebView页面?我认为这不是刷新WebView页面的好方法。还有别的办法吗 WebView.swift打开模式自定义警报Ios 在swift5中关闭页面时如何传输参数?,ios,swift,parameter-passing,Ios,Swift,Parameter Passing,我目前正在研究Swift5。我有个问题。目前有一个WebView启动和一个自定义通知窗口来检查数据。检查数据后,我将关闭通知窗口 当我关闭此处的通知窗口时,如何将数据转发到WebView页面?我认为这不是刷新WebView页面的好方法。还有别的办法吗 WebView.swift打开模式自定义警报 let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil) 将myAlert=storyboard.InstanceEviewController(标识符为:“Chec
let storyboard=UIStoryboard(名称:“Main”,捆绑包:nil)
将myAlert=storyboard.InstanceEviewController(标识符为:“CheckAlertController”)设置为!CheckAlertController
myAlert.modalPresentationStyle=.overCurrentContext
myAlert.ModAltTransitionStyle=.crossDissolve
self.present(myAlert,动画:false,完成:nil)
模式自定义警报。swift
@iAction func-okButton(u发件人:任意){
self.disclose(动画:true,完成:nil)
}
我想在按下OK按钮并关闭模式通知窗口时传递数据
我怎样才能解决这个问题
还有别的办法吗?正确的方法是什么
例如,我接收在自定义警报窗格的文本字段中输入的数据
var callBack: (()->())?
@IBAction func okButton(_ sender: Any) {
callBack?()
self.dismiss(animated: true, completion: nil)
}
var callBack: ((_ dataToPass: String)->())?
@IBAction func okButton(_ sender: Any) {
//1 step: you will pass you string throw the closure, so you will catch it in your WebView
callBack?(textToPass)
// 2 step: closing the window as a later event will not interrupt passing data above
self.dismiss(animated: true, completion: nil)
}
WebView.swift
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myAlert = storyboard.instantiateViewController(withIdentifier: "CheckAlertController") as! CheckAlertController
myAlert.callBack = {
// Execute your code
}
myAlert.modalPresentationStyle = .overCurrentContext
myAlert.modalTransitionStyle = .crossDissolve
self.present(myAlert, animated: false, completion: nil)
将此代码添加到模态视图控制器的顶部
protocol CheckAlertControllerDelegate: class {
func handleData(_ data: String) // String for example
}
weak var delegate: CheckAlertControllerDelegate?
在模态视图控制器内的任意位置添加此代码
protocol CheckAlertControllerDelegate: class {
func handleData(_ data: String) // String for example
}
weak var delegate: CheckAlertControllerDelegate?
修改“确定”按钮
@IBAction func okButton(_ sender: Any) {
self.dismiss(animated: true, completion: {
delegate?.handleData("Your data here")
})
}
在您的网页中,在打开模式之前添加此行
myAlert.delegate = self
self.present(myAlert, animated: false, completion: nil)
并将其添加到您的网页中
extension YourWebPageViewController: CheckAlertControllerDelegate {
func handleData(_ data: String) {
}
}
这个答案与@Jaydeep left的答案非常相似,只是为了向@hongDeep更详细地解释一下
模式自定义警报.swift
var callBack: (()->())?
@IBAction func okButton(_ sender: Any) {
callBack?()
self.dismiss(animated: true, completion: nil)
}
var callBack: ((_ dataToPass: String)->())?
@IBAction func okButton(_ sender: Any) {
//1 step: you will pass you string throw the closure, so you will catch it in your WebView
callBack?(textToPass)
// 2 step: closing the window as a later event will not interrupt passing data above
self.dismiss(animated: true, completion: nil)
}
WebView.swift
...
let myAlert = storyboard.instantiateViewController(withIdentifier: "CheckAlertController") as! CheckAlertController
// defining a closure to catch a callback
myAlert.callBack = { dataToPass in // define dataToPass in closure`s capture list
// Handle your code
}
...
对不起,你能给我举个回调的例子吗?我想不出该怎么办。例如,我收到了在自定义警报的文本字段中输入的数据pane@hongdevelop在这种情况下,您应该只在闭包中添加所需的参数:var回调:((字符串)->())?然后您可以在okButton函数回调(textToPass)中传递数据。在WebView中,只需添加myAlert.callBack={textToPass In//handle here}如果我关闭模式,我会进入回调吗?@hongdevelop不确定您的意思:作为第一步,回调将调用并在WebView中捕获结果,并且只有在这之后,您将关闭视图-因此,如果您担心此问题,它将不会中断WebView中关闭内部的代码。我需要从web视图屏幕接收数据,而不是从网页接收数据。例如,我接收我在自定义警报窗格中的文本字段中输入的数据,ok。我更新了我的答案。只是有点误会谢谢你的充分解释。