0

我正在申请初级开发人员职位,我有一个非常具体的任务,我已经花了 3 天时间完成。听起来很简单 - 将数据传递给 rootViewController。这就是我所做的:

1)

   private func userDefaultsToRootController() {
        let input = textField.text!
        defaults.set(input, forKey: "SavedLabel")
        navigationController?.popViewController(animated: true)
    } 
  1. 私人 func segueToRootViewController() { 让destinationVC = MainScreen1() 让输入 = 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)
}
  1. 我用过CoreData

但这是困难的部分 - 我收到一封电子邮件,所有这些方法都不够好,我需要使用委托和闭包。我以前做过委托和闭包,但是当我 popToRootViewController 委托方法通过 nil 时。您能否至少指出在哪里可以找到有关此的信息?

** 添加 **

有 2 个视图控制器:初始和第二个。这就是我在初始视图控制器中的内容:

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
        
    }
}

代表没有被调用。

4

1 回答 1

1

根据您的编辑:

您必须将委托设置为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)
      }
}
于 2020-06-23T13:38:56.873 回答