[英]How to segue (push) from ViewController to another ViewController embedded in NavigationController?
I have a tableView in a viewController and when I tap on a cell a push segue is performed to a navigation controller that is embedded in another viewController. 我在viewController中有一个tableView,当我点击一个单元格时,将对嵌入在另一个viewController中的导航控制器执行推送选择。 In the swift file for the first VC I have
prepareForSegue
function firing when a cell is tapped and in that function I pass some info to a textView that is in the second viewController. 在第一个VC的swift文件中,当点按一个单元格时,我具有
prepareForSegue
函数,在该函数中,我将一些信息传递给第二个viewController中的textView。
But when I run my app I get an error when I tap on a cell because I cast the destination VC as the second VC not the navigationController. 但是,当我运行我的应用程序时,点击单元格会出现错误,因为我将目标VC转换为第二个VC,而不是navigationController。 But if I cast it as the navigationController instead, then the
segue
works but I cannot pass the info from the first VC to the textView in the second VC. 但是,如果我将其
segue
为navigationController,则segue
可以工作,但是我无法将信息从第一个VC传递到第二个VC中的textView。 Am I missing something here? 我在这里想念什么吗?
You should reach the Navigation Controller and get the topViewController from stack. 您应该到达导航控制器,并从堆栈中获取topViewController。 This code snippet should be work.
此代码段应该有效。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "yourSegueIdentifierIsHere" {
let destinationVC = segue.destinationViewController.navigationController?.topViewController as? YourClass
destinationVC. // do something.
}
}
Second question answer is : 第二个问题的答案是:
You can get the indexPath like this; 您可以像这样获取indexPath;
let indexPath = tableView.indexPathForSelectedRow
and you can use yourArray[indexPath.row]
您可以使用
yourArray[indexPath.row]
Third question answer is; 第三个问题的答案是;
You can not directly set your textView.text in your prepareForSegue method. 您不能在prepareForSegue方法中直接设置textView.text。 You should create a variable in your destionationViewController and you can send the data to this variable.
您应该在destionationViewController中创建一个变量,然后可以将数据发送到该变量。 Create a variable in your destionationViewController like this;
像这样在您的destionationViewController中创建一个变量;
var journalText: String = ""
and now send the data in your prepareForSegue method. 然后通过您的prepareForSegue方法发送数据。
destinationVC.journalText = "Some string"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.