我正在申请一个初级开发人员的职位,我有一个非常具体的任务,这已经花了我3天的时间来完成。 听起来很容易--将数据传递给RootViewController。 我就是这么做的:
1)
private func userDefaultsToRootController() {
let input = textField.text!
defaults.set(input, forKey: "SavedLabel")
navigationController?.popViewController(animated: true)
}
私有函数segueToRootViewController(){let destinationVC=MainScreen1()let input=TextField.text!
if input == "" { self.navigationController?.popToRootViewController(animated: true) }
destinationVC.input = input
navigationController?.pushViewController(destinationVC, animated: true)
}
private func popToNavigationController() {
let input = textField.text!
if let rootVC = navigationController?.viewControllers.first as? MainScreen1 {
rootVC.input = input
}
navigationController?.popToRootViewController(animated: true)
}
但这是困难的部分--我收到一封电子邮件,所有这些方法都不够好,我需要使用delegate和closure。 我以前做过委托和闭包,但是当我弹出ToRootViewController时,委托方法传递了nil。 你能不能至少指出在哪里能找到这方面的信息?
**增加**
有两个视图控制器:初始视图控制器和第二视图控制器。 这就是我在初始视图控制器中的内容:
var secondVC = MainScreen2()
override func viewDidLoad() {
super.viewDidLoad()
secondVC.delegate = self
}
这就是我推SecondViewController的方式
@objc private func buttonTapped(_ sender: CustomButton) {
let nextViewController = MainScreen2()
navigationController?.pushViewController(nextViewController, animated: true)
}
在SecondViewController中,我得到了这个协议
protocol PassData {
func transferData(text: String)
}
也是代表:
var delegate: PassData?
这就是我如何回到初始视图控制器
@objc private func buttonTapped(_ sender: CustomButton) {
if let input = textField.text {
print(input)
self.delegate?.transferData(text: input)
self.navigationController?.popToRootViewController(animated: true)
}
}
回到我实现委托方法的初始视图控制器
extension MainScreen1: PassData {
func transferData(text: String) {
print("delegate called")
label.text = text
}
}
不调用委托。
基于您的编辑:
必须在ButtonTapped
中设置委托
@objc private func buttonTapped(_ sender: CustomButton) {
let nextViewController = MainScreen2()
nextViewController.delegate = self // HERE WHERE YOU SET THE DELEGATE
navigationController?.pushViewController(nextViewController, animated: true)
}
您可以在ViewDidLoad
中删除第二个实例和您的代码。 那不是你推的例子。
这将为您指明使用委托和完成处理程序的正确方向。
protocol YourDelegateName {
func passData(data:YourDataType)
}
class SecondViewController: UIViewController {
var delegate: YourDelegateName?
func passDataFromSecondViewController(){
YourCoreDataClass.shared.getCoreData { (yourStringsArray) in
self.delegate?.passData(data: yourStringsArray)
self.navigationController?.popToRootViewController(animated: true)
}
}
class InitialViewController: UIViewController, YourDelegateName {
override func viewDidLoad() {
super.viewDidLoad()
// or whenever you instantiate your SecondViewController
let secondViewController = SecondViewController()
secondViewController.delegate = self //VERY IMPORTANT, MANY MISS THIS
self.navigationController?.pushViewController(createVC, animated: true)
}
func passData(data:YourDataType){
//user your data
}
}
class YourCoreDataClass: NSObject {
static let shared = YourCoreDataClass()
func getCoreData (completion: ([String]) -> ()){
........... your code
let yourStringsArray = [String]() // let's use as example an array of strings
//when you got the data your want to pass
completion(yourStringsArray)
}
}