【发布时间】:2021-11-07 08:02:40
【问题描述】:
我使用自定义 XIB 文件作为视图控制器的弹出框。 XIB 文件包含两个按钮 =“发送”和“添加附加字段”。选择添加其他字段的按钮会显示另一个嵌入在导航控制器中的视图控制器。当用户登陆该控制器屏幕并选择他们的额外选项时,该数据应传回 XIB 的var extraOptions = [String]() 字段。从那里,用户应该能够通过选择“发送”来上传他们的帖子。
我遇到的问题是数据没有从嵌入在导航控制器中的视图控制器传回 XIB。
这甚至可能吗?我已经尝试了所有我能想到的解决方案:协议/委托方法,在 XIB 文件中设置 var extraOptions 字段,从呈现的控制器开始,然后将其关闭并返回 XIB,这是我的最新尝试:
名为CreatePostModal的自定义XIB(弹出的视图控制器)
@objc func addExtraOptions(_ sender: UITapGestureRecognizer? = nil) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UINavigationController = storyboard.instantiateViewController(withIdentifier: "ExtraOptionsNavigationController") as! UINavigationController
vc.delegate = self
vc.modalPresentationStyle = .fullScreen
self.present(vc, animated: true, completion: nil)
}
ExtraOptionsViewController
@IBAction func donePressed(_ sender: Any) {
if let navController = presentingViewController as? UINavigationController {
let presenter = navController.topViewController as! CreatePostModal
presenter.extraOptions = self.extraOptionsSelected
print("These are the options selected ==> \(present.extraOptions)") //this prints the options and shows that they're there, but the array is empty whenever a user tries to upload the data
}
dismiss(animated: true, completion: nil)
}
如您所见,在 print 方法中,它打印变量并显示数据在那里,但是当返回 XIB 文件,并且用户保存并上传数据时,该字段显示为空。
如何将数据从嵌入导航控制器的视图传回 XIB?
【问题讨论】:
-
使用通知。
-
如果您想使用委托,导航控制器需要将委托注入 VC,这需要自定义导航控制器。然后可以将数据传回委托链。
标签: arrays swift database xcode xib