ios Swift : prepareForSegue 与导航控制器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28788416/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift : prepareForSegue with navigation controller
提问by cedric petetin
I am developing an iOS application in Swift.
我正在 Swift 中开发一个 iOS 应用程序。
I want to send data from a view to an other one, using the prepareForSegue function.
我想使用 prepareForSegue 函数将数据从一个视图发送到另一个视图。
However, my target view is preceded by a navigation controller, so it doesn't work.
但是,我的目标视图前面有一个导航控制器,所以它不起作用。
How can I do this please ?
请问我该怎么做?
回答by Wojtek Surowka
In prepareForSegue
access the target navigation controller, and then its top:
在prepareForSegue
访问目标导航控制器,然后它的顶部:
let destinationNavigationController = segue.destination as! UINavigationController
let targetController = destinationNavigationController.topViewController
From the target controller you can access its view and pass data.
您可以从目标控制器访问其视图并传递数据。
In old - now obsolete - versions of Swift and UIKit the code was slightly different:
在旧的 - 现在已经过时 - 版本的 Swift 和 UIKit 中,代码略有不同:
let destinationNavigationController = segue.destinationViewController as UINavigationController
let targetController = destinationNavigationController.topViewController
回答by mattyU
Prepare the segue in the SendViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "segueShowNavigation" { if let destVC = segue.destination as? UINavigationController, let targetController = destVC.topViewController as? ReceiveViewController { targetController.data = "hello from ReceiveVC !" } } }
Edit the identifier segue to "showNavigationController"
在 SendViewController 中准备 segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "segueShowNavigation" { if let destVC = segue.destination as? UINavigationController, let targetController = destVC.topViewController as? ReceiveViewController { targetController.data = "hello from ReceiveVC !" } } }
将标识符 segue 编辑为“showNavigationController”
- In your ReceiveViewController add
- 在您的 ReceiveViewController 添加
this
这个
var data : String = ""
override func viewDidLoad() {
super.viewDidLoad()
print("data from ReceiveViewController is \(data)")
}
Of course you can send any other type of data (int, Bool, JSON ...)
当然,您可以发送任何其他类型的数据(int、Bool、JSON ...)
回答by jason z
Complete answer using optional binding
and Swift 3 & 4:
使用optional binding
Swift 3 & 4完成答案:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let navigationVC = segue.destination as? UINavigationController, let myViewController = navigationVC.topViewController as? MyViewControllerClass {
myViewController.yourProperty = myProperty
}
}
回答by JoeGalind
A one liner in Swift 3:
Swift 3 中的单行代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination.childViewControllers[0] as? FooController {
vc.variable = localvariable
}
}
回答by Ondrej Kvasnovsky
Here is the answer for Swift 3:
这是 Swift 3 的答案:
let svc = segue.destination as? UINavigationController
let controller: MyController = svc?.topViewController as! MyController
controller.myProperty = "Hi there"
回答by John Pitts
In Swift 5
在斯威夫特 5
If you must not only segue from a SourceViewController to a DestinationViewController embedded in a UINavigationController, but also to a new Storyboard also, then do the following...
如果您不仅必须从 SourceViewController 转到嵌入在 UINavigationController 中的 DestinationViewController,而且还必须转到新的 Storyboard,请执行以下操作...
Place a "Storyboard Reference" object from your Object Library next to your source ViewController in Interface Builder, and then drag a segue to it (from a button on the SourceViewController view, for instance). Name the segue identifier "ToOtherStoryboard", for example.
Go to NavigationViewController and give it a Storyboard ID using the Identity Inspector. "DestinationNavVC" would do.
Click the Storyboard Reference icon you created in step 1, and in its attribute inspector's 'Referenced ID' field, enter the Storyboard ID you wrote for the UINavigationController in step 2. This creates the segue from source to the DestinationViewController no matter what you write in source file of the source ViewController. This is because seguing to a NaviationController will automatically show the root ViewController (the first one) of the UINavigationController.
(OPTIONAL) If you need to attach data along with your segue and send it to properties within the DestinationViewController, you would write the following code inside a Prepare-For-Segue method in your SourceViewController file:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "ToOtherStoryboard" { let destinationNavVC = segue.destination as! UINavigationController let destinationVC = destinationNavVC.topController as! DestinationViewController destinationVC.name = nameTextField.text // for example destinationVC.occupation = occupationTextField.text } }
You do not NEED to have a PrepareForSegue if you're simply trying to move from one ViewController to another, the methods above will work (w/o step 3)
In your IBAction Outlet method for your button you used to initiate the segue, you would write:
performSegue(withIdentifer: "ToOtherStoryboard", sender: self)
将对象库中的“Storyboard Reference”对象放置在 Interface Builder 中源 ViewController 旁边,然后将 segue 拖到它上面(例如,从 SourceViewController 视图上的按钮)。例如,将 segue 标识符命名为“ToOtherStoryboard”。
转到 NavigationViewController 并使用 Identity Inspector 为其提供 Storyboard ID。“DestinationNavVC”就可以了。
单击您在第 1 步中创建的 Storyboard Reference 图标,然后在其属性检查器的“Referenced ID”字段中,输入您在第 2 步中为 UINavigationController 编写的 Storyboard ID。无论您编写什么,这都会创建从源到 DestinationViewController 的转场源视图控制器的源文件。这是因为连接到 NaviationController 将自动显示 UINavigationController 的根 ViewController(第一个)。
(可选)如果您需要将数据与 segue 一起附加并将其发送到 DestinationViewController 中的属性,您可以在 SourceViewController 文件的 Prepare-For-Segue 方法中编写以下代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { if segue.identifier == "ToOtherStoryboard" { let destinationNavVC = segue.destination as! UINavigationController let destinationVC = destinationNavVC.topController as! DestinationViewController destinationVC.name = nameTextField.text // for example destinationVC.occupation = occupationTextField.text } }
如果您只是想从一个 ViewController 移动到另一个 ViewController,则不需要 PrepareForSegue,上面的方法将起作用(没有第 3 步)
在用于启动转场的按钮的 IBAction Outlet 方法中,您将编写:
performSegue(withIdentifer: "ToOtherStoryboard", sender: self)
回答by Alan Silva
Set the identifier name in the segue arrow property in order to use in the the performeSegue.
在 segue 箭头属性中设置标识符名称以便在 performeSegue 中使用。
Like this:
像这样:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc: ProfileViewController = segue.destination as? ProfileViewController {
//do any setting to the next screen
}
}
and then:
进而:
performSegue(withIdentifier: "yourIdentifierOfViewProfile", sender: indexPath.row)
I hope it helps.
我希望它有帮助。