简体   繁体   English

如何从ViewController选择(推送)到NavigationController中嵌入的另一个ViewController?

[英]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.

相关问题 当segue到另一个ViewController时,TabBar嵌入式NavigationController栏会消失 - TabBar embedded NavigationController bar disappears when segue to another ViewController 如何从嵌入在TabController中的NavigationController内的ViewController访问tabBarItem? - How to access tabBarItem from ViewController inside of NavigationController embedded in TabController? 如何从一个视图控制器转移到另一个视图控制器 - How to segue from one viewcontroller to another 从TableViewCell跳转到NavigationController中嵌入的ViewController - Jump from a TableViewCell to a ViewController embedded in a NavigationController 从嵌入在TabBarController和NavigationController中的ViewController推送新的ViewController - Pushing a new ViewController from a ViewController that is embedded in a TabBarController and NavigationController 如何从一个navigationcontroller推送到多个viewcontroller? - How do I push from one navigationcontroller to multiple viewcontroller? 如何将值传递给嵌入在 NavigationController 中的 viewController - How to pass value to a viewController embedded in NavigationController 从侧面菜单中在NavigationController中推送ViewController - Push ViewController in NavigationController from side menu 查找数据并将数据从ViewController传输到另一个ViewController - segue and transfer data from viewcontroller to viewcontroller to another viewcontroller 从UITableViewCell到另一个ViewController - Segue from UITableViewCell to another ViewController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM