如何将表视图中的选定行字符串传递给另一个ViewController页面? (迅捷2) [英] How to pass selected row string in table view to another ViewController page? (Swift 2)

查看:87
本文介绍了如何将表视图中的选定行字符串传递给另一个ViewController页面? (迅捷2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行Xcode 7.3并使用Swift 2.2.我在将数据从第一个视图控制器传递到第二个视图控制器时遇到麻烦.我在第一个视图控制器中创建了一个变量string,该变量保存了表视图中的选定行,并且可以正常工作.我还在第二个视图控制器中创建了另一个变量string,该变量应保留所选行的名称,然后将文本传递给标签.

I am running Xcode 7.3 and using Swift 2.2. I am having trouble passing data from my first view controller to the second view controller. I've created a variable string in my first view controller that holds the selected row in the tableview and it works. I have also created another variable string in my second view controller that should hold the name of the selected row, then pass the text to a label.

import UIKit
import Parse
import ParseUI

class FruitListPage: UIViewController, UITableViewDelegate, UITableViewDataSource {


var selectedFruit = String()

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    let fruitName:Scanner2 = self.storyboard?.instantiateViewControllerWithIdentifier("scanPage") as! Scanner2
    fruitName.loadView()
    let nameShop = textArray[indexPath.row]
    print(nameShop)
    selectedFruit = nameShop

    print(selectedFruit)

    fruitName.selectedFruit2 = selectedFruit

}

在第一个视图控制器中,一切在这里都可以正常工作,每当我在表格视图中按一行时,我都可以看到水果名称.真正的问题是第二个视图控制器中的selectedFruit2字符串从不从第一个视图控制器中获取数据.有任何线索吗?

Everything works completely fine unit here in the first view controller and I can see the fruit name every time I press a row in the tableview. The real problem is that selectedFruit2 string in the second view controller never grabs the data from the first view controller. Any clue?

import UIKit
import Parse

class Scanner2: UIViewController {

@IBOutlet var fruitNameLabel: UILabel!

var selectedFruit2 = String()

override func viewDidLoad() {
    super.viewDidLoad()

    print(selectedFruit2)

}

}

推荐答案

使用segue在storyBoard中连接您的viewController,然后将其标识符设置为"scanPageSegue" ...然后:

Connect your viewController in storyBoard with a segue and set its identifier as "scanPageSegue" for instance... then:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    performSegueWithIdentifier("scanPageSegue", sender: textArray[indexPath.row])
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "scanPageSegue" {
        if let fruitName = segue.destinationViewController as? Scanner2, 
           selectedFruit = sender as? String {
            fruitName.selectedFruit2 = selectedFruit
        }
    }
}

这篇关于如何将表视图中的选定行字符串传递给另一个ViewController页面? (迅捷2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆