使用 popToRootViewController 并传递数据
use popToRootViewController and pass Data
我正在申请初级开发人员职位,我有一项非常具体的任务,我已经花了 3 天时间才能完成。听起来很简单——将数据传递给 rootViewController。
这就是我所做的:
1)
private func userDefaultsToRootController() {
let input = textField.text!
defaults.set(input, forKey: "SavedLabel")
navigationController?.popViewController(animated: true)
}
private 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)
}
- 我用过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
}
}
代理未被调用。
根据您的编辑:
您必须在 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)
}
}
我正在申请初级开发人员职位,我有一项非常具体的任务,我已经花了 3 天时间才能完成。听起来很简单——将数据传递给 rootViewController。 这就是我所做的:
1)
private func userDefaultsToRootController() {
let input = textField.text!
defaults.set(input, forKey: "SavedLabel")
navigationController?.popViewController(animated: true)
}
private 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)
}
- 我用过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
}
}
代理未被调用。
根据您的编辑:
您必须在 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)
}
}