简体   繁体   English

使用Swift和Storyboard在两个UIViewController之间传递数据

[英]Passing data between two UIViewControllers using Swift and Storyboard

I was using below code to selected UITableView to pass data between UIViewControllers 我正在使用下面的代码选择UITableView在UIViewControllers之间传递数据

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        RecipeDetailViewController *destViewController = segue.destinationViewController;
        destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
    }
}

How to do same thing using swift with tableView selected data? 如何使用swift与tableView所选数据进行相同的操作?

I have tried below approach for simple passing which is working 我已经尝试了以下方法进行有效的简单通过
but How to replicate above mentioned case. 但是如何复制上述情况。

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

    var detailsVC = segue.destinationViewController as SecondViewController

    detailsVC.passedString = "hello"

}

This should work: 这应该工作:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "showRecipeDetail" {

            var detailsVC = segue.destinationViewController as SecondViewController
            let indexPath = tableView.indexPathForSelectedRow()
            detailsVC.passedString = recipes![indexPath?row]
            //or try that if above doesn't work: detailsVC.passedString = recipes[indexPath?row]
        }
    }
}

This line could require amendments: 此行可能需要修改:

destViewController?.destViewController.recipeName = recipes![i]

it depends is you properties optional or not (maybe you have to remove ? or !) 这取决于您的属性是否可选(也许您必须删除?或!)

Try this... If you're first view controller is embedded inside Navigation Controller, you can push the second view controller on didSelectRowAtINdexPath delegate, as below... 尝试此操作...如果您是第一个视图控制器嵌入在Navigation Controller内,则可以将第二个视图控制器推入didSelectRowAtINdexPath委托上,如下所示...

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Create a storyboard instance.....
    var storyBoard = UIStoryboard(name: "Main", bundle: nil)
    // Create a SecondViewController instance, from the storyboardID of the same.
    var seconVC = storyBoard.instantiateViewControllerWithIdentifier("seconViewControllerID") as SecondViewController
    seconVC.passedString = receipes[indexPath.row] as String
    self.navigationController?.pushViewController(secondVC, animated: true)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用情节提要在UIViewController之间切换 - Unable to switch between UIViewControllers using storyboard 在两个UIViewControllers之间进行编程转换(Xcode 9,Swift 4) - Programmatically transitioning between two UIViewControllers (Xcode 9, Swift 4) Swift中一个屏幕上的两个UIViewController之间的交互 - Interaction between two UIViewControllers on one screen in Swift 从UIDatePicker在UIViewControllers之间传递数据 - passing data between UIViewControllers from UIDatePicker 如何使用didSelectRowAtIndexPath(在Swift中)在两个故事板之间传递日期? - How to passing Date between two Storyboard with didSelectRowAtIndexPath(in Swift)? 在Swift 3中在不使用情节提要的情况下使用didSelect和navigationController在TableViewControllers。之间传递数据 - Passing data between TableViewControllers.using didSelect and navigationController without storyboard in Swift 3 如何使用Storyboard在iOS中的UIViewControllers之间切换 - How can I switch between UIViewControllers in iOS using Storyboard 在UIViewControllers之间传递NSFetchedResultsController - Passing NSFetchedResultsController between UIViewControllers 使用Swift通过View控制器传递数据而不使用Storyboard - Passing Data through View Controllers with Swift without Using Storyboard 两个UIViewController之间的UIPageViewController - UIPageViewController between two UIViewControllers
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM