[英]Pass data backward from detailViewController to masterViewController
I am trying to pass data back from the second viewController. 我试图从第二个viewController传回数据。 I can do that without NavigationController.
我可以在没有NavigationController的情况下做到这一点。 But now I need to use NavigationController.
但是现在我需要使用NavigationController。 Then my code does work as before.
然后我的代码就可以像以前一样工作了。 The data wont pass.
数据不会通过。 Here is the simple code: In first viewController
这是简单的代码:在第一个viewController中
class ViewController: UIViewController, backfromSecond {
@IBOutlet weak var text: UILabel!
var string : String?
override func viewDidLoad() {
super.viewDidLoad()
self.string = "Start here"
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
self.text.text = self.string
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? secondViewController{
destinationViewController.delegate = self
}
}
func back(text: String) {
self.string = text
print(text)
}
}
And Second viewController: 第二个viewController:
protocol backfromSecond {
func back(text: String)
}
class secondViewController: UIViewController {
var string : String = "nothing here"
var delegate : backfromSecond?
override func viewDidLoad() {
super.viewDidLoad()
delegate?.back(text: string)
// Do any additional setup after loading the view.
}
}
What is wrong here? 怎么了
I think your problem is in the prepare for segue method. 我认为您的问题在于准备segue方法。 If the view controller is on a navigation stack i think your code should be something like
如果视图控制器在导航堆栈上,我认为您的代码应类似于
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? UINavigationController).topViewController as! secondViewController{
destinationViewController.delegate = self
}
}
This works me well. 这对我来说很好。
1st VC 第一风投
class ViewController: UIViewController, backfromSecond {
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func Passingfrom1stVCTo2ndVC(_ sender: AnyObject) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController3") as? ViewController3{
vc.dataFrom1StVC = "message send from 1st VC"
vc.delegate = self
self.navigationController?.pushViewController(vc, animated: true)
}
}
func back(text: String) {
print("data\(text)")
}
}
2nd VC. 第二个VC。
protocol backfromSecond: class {
func back(text: String)
}
class ViewController3: UIViewController {
var dataFrom1StVC : String? = nil
week var delegate : backfromSecond?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func DataSendFrom2ndVCTo1stVC(_ sender: AnyObject) {
self.delegate?.back(text: "Message Send From 2nd vc to 1st VC")
self.navigationController?.popViewController(animated: true)
}
}
I hope it will work you. 希望对您有用。 If any problem then ask me i will help you.
如果有任何问题,请问我,我将为您提供帮助。
You can use unwind segues to pass data back. 您可以使用展开序列将数据传回。 Here's a tutorial https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/
这是一个教程https://spin.atomicobject.com/2014/10/25/ios-unwind-segues/
Suppose A & B are two controllers and you first navigated from A to B with some data. 假设A和B是两个控制器,您首先使用一些数据从A导航到B。 And now you want to POP from B to A with some data.
现在,您想使用一些数据从B POP到A POP。
Unwind Segues is the best and recommended way to do this. 放松Segues是执行此操作的最佳建议。 Here are the steps.
步骤如下。
define following method 定义以下方法
@IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) { @IBAction func unwindSegueFromBtoA(segue:UIStoryNoardSegue){
} }
open storyboard 打开情节提要
Select B ViewController and click on ViewController outlet. 选择B ViewController,然后单击ViewController插座。 press control key and drag to 'Exit' outlet and leave mouse here.
按下控制键并拖动到“退出”出口,然后将鼠标留在此处。 In below image, selected icon is ViewController outlet and the last one with Exit sign is Exit Outlet.
在下图中,选中的图标是ViewController插座,最后一个带有Exit标志的图标是Exit Outlet。
You will see 'unwindSegueFromBtoA' method in a popup . 您将在弹出窗口中看到“ unwindSegueFromBtoA”方法。 Select this method .
选择此方法。
Now you will see a segue in your view controler hierarchy in left side. 现在,您将在左侧的视图控制器层次结构中看到一个序列。 You will see your created segue near StoryBoard Entry Piont in following Image.
下图将在StoryBoard Entry Piont附近看到您创建的序列。
Select this and set an identifier to it. 选择它并为其设置标识符。 (suggest to set the same name as method - unwindSegueFromBtoA)
(建议设置与方法相同的名称-unwindSegueFromBtoA)
Open Bm . 打开Bm。 Now, wherever you want to pop to A. use
现在,无论您要弹出A的哪个位置,都可以使用
self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend) self.performSegueWithIdentifier(“ unwindSegueFromBtoA”,发送者:dataToSend)
Now when you will pop to 'A', 'unwindSegueFromBtoA' method will be called. 现在,当您弹出到“ A”时,将调用“ unwindSegueFromBtoA”方法。 In unwindSegueFromBtoA of 'A' you can access any object of 'B'.
在“ A”的unwindSegueFromBtoA中,您可以访问“ B”的任何对象。
That's it..! 而已..!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.