Ios 无法使用segue[Swift]在第二视图控制器中显示数据

Ios 无法使用segue[Swift]在第二视图控制器中显示数据,ios,swift,segue,Ios,Swift,Segue,我需要将数据从一个视图控制器传递到另一个视图控制器。我使用segue(detail)并定义了一个名为“Photo”的模型类 TableViewController如下所示: var photos = [Photo]() //strongly typed swift array override func viewDidLoad() { super.viewDidLoad() var newPhoto = Photo(name:"cat ", file

我需要将数据从一个视图控制器传递到另一个视图控制器。我使用segue(detail)并定义了一个名为“Photo”的模型类

TableViewController如下所示:

var photos = [Photo]()  //strongly typed swift array

    override func viewDidLoad() {
        super.viewDidLoad()

        var newPhoto = Photo(name:"cat ", fileName:"cat", notes:"cat_file")
        photos.append(newPhoto)

        var newPhoto2 = Photo(name:"r2 ", fileName:"r2", notes:"r2")
        photos.append(newPhoto2)


    }
import UIKit

class PhotoDiplayViewController: UIViewController {

    var currentPhoto: Photo?


    @IBOutlet weak var currentImage: UIImageView!


    @IBOutlet weak var currentLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        var image = UIImage(named: currentPhoto!.fileName)
        self.currentImage.image = image

        self.currentLabel.text = currentPhoto?.name
        println(currentPhoto!.name  + currentPhoto!.fileName + currentPhoto!.notes)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
另一个视图控制器(DetailViewController)如下所示:

var photos = [Photo]()  //strongly typed swift array

    override func viewDidLoad() {
        super.viewDidLoad()

        var newPhoto = Photo(name:"cat ", fileName:"cat", notes:"cat_file")
        photos.append(newPhoto)

        var newPhoto2 = Photo(name:"r2 ", fileName:"r2", notes:"r2")
        photos.append(newPhoto2)


    }
import UIKit

class PhotoDiplayViewController: UIViewController {

    var currentPhoto: Photo?


    @IBOutlet weak var currentImage: UIImageView!


    @IBOutlet weak var currentLabel: UILabel!


    override func viewDidLoad() {
        super.viewDidLoad()

        var image = UIImage(named: currentPhoto!.fileName)
        self.currentImage.image = image

        self.currentLabel.text = currentPhoto?.name
        println(currentPhoto!.name  + currentPhoto!.fileName + currentPhoto!.notes)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}
当我运行程序时,表视图加载良好,如果我单击任何单元格,它将进入详细视图控制器。但细节视图控制器中没有任何内容。我使用println()进行检查,调试器中的输出如下所示:

cat cat cat_file
为了传递数据,我使用了以下segue代码块:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        var secondScene = segue.destinationViewController as! PhotoDiplayViewController

        if let indexPath = self.tableView.indexPathForSelectedRow(){
            let selectedPhoto  = photos[indexPath.row]
            secondScene.currentPhoto = selectedPhoto
        }

    }
但还是没有运气!试图找出我失踪的地方?有人能告诉我我在哪吗

更新:完成详细视图控制器类别代码

更新:我的表视图代码的完整详细信息

import UIKit

class PhotoTableViewController: UITableViewController {

    var photos = [Photo]()  //strongly typed swift array

    override func viewDidLoad() {
        super.viewDidLoad()

        var newPhoto = Photo(name:"cat ", fileName:"cat", notes:"cat_file")
        photos.append(newPhoto)

        var newPhoto2 = Photo(name:"r2 ", fileName:"face.jpg", notes:"r2")
        photos.append(newPhoto2)


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return photos.count
    }


    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("photoCell", forIndexPath: indexPath) as! UITableViewCell

        var currentPhoto = photos[indexPath.row]
        cell.textLabel?.text = currentPhoto.name

        return cell
    }


    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return NO if you do not want the item to be re-orderable.
        return true
    }
    */


    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using [segue destinationViewController].
        // Pass the selected object to the new view controller.
        var secondScene = segue.destinationViewController as! PhotoDiplayViewController

        if let indexPath = self.tableView.indexPathForSelectedRow(){
            let selectedPhoto  = photos[indexPath.row]
            secondScene.currentPhoto = selectedPhoto
        }

    }


}

不要使用segue。使用这个,它更容易

遵循以下步骤

1:创建一个名为Manager.swift的单独文件,并将此代码放入其中

//manager.swift

    import Foundation

    struct Manager {

    static var dataToPass = String()


    }
2:按Shift+Command+K键清理项目

3:在第一个视图中,控制器将dataToPass设置为要传递的数据

Manager.dataToPass = self.dataToPass
4:在第二个视图中,控制器检索数据并将内容设置为dataToPass

self.dataToReceive = Manager.dataToPass

5:你完了

不要使用segue。使用这个,它更容易

遵循以下步骤

1:创建一个名为Manager.swift的单独文件,并将此代码放入其中

//manager.swift

    import Foundation

    struct Manager {

    static var dataToPass = String()


    }
2:按Shift+Command+K键清理项目

3:在第一个视图中,控制器将dataToPass设置为要传递的数据

Manager.dataToPass = self.dataToPass
4:在第二个视图中,控制器检索数据并将内容设置为dataToPass

self.dataToReceive = Manager.dataToPass

5:你完了

我展示的代码在从情节提要中删除所有图像视图和标签并重新映射后完全正常工作。但我想知道问题出在哪里。但是,我想分享一个屏幕截图:

在屏幕截图中,您将看到3个组件:2个标签和1个图像视图。一个1标签的文本为深黑色,但另两个标签的文本与它们不同。所有这些都已正确配置

我还是不知道为什么会这样?我不确定。。。。可以在故事板的顶部添加一些隐藏的组件吗???还是Xcode的一个bug

然而,如果你有类似的经验,请分享。我的目标不仅是解决问题,而且是了解问题的原因


:)

在从情节提要中删除所有图像视图和标签并重新映射后,我展示的代码完全正常工作。但我想知道问题出在哪里。但是,我想分享一个屏幕截图:

在屏幕截图中,您将看到3个组件:2个标签和1个图像视图。一个1标签的文本为深黑色,但另两个标签的文本与它们不同。所有这些都已正确配置

我还是不知道为什么会这样?我不确定。。。。可以在故事板的顶部添加一些隐藏的组件吗???还是Xcode的一个bug

然而,如果你有类似的经验,请分享。我的目标不仅是解决问题,而且是了解问题的原因



:)

仔细检查图像名称,是否匹配?是否可以共享整个DetailViewController类?它看起来像这样吗?类PhotoDisplayViewController:UIViewController{…?在PhotoDisplayViewController类中,我建议向didViewLoad()添加类似的内容,以确保传递对象。如果让myPhoto=currentPhoto{..@Esric Qian:图像名称匹配正确。我试图检查是否匹配,控制台输出是否在那里。此外,文本字段也没有显示在那里:(代码看起来很好。您是否检查了
PhotoDiplayViewController
中的
image
是否为非零且已正确加载?您是否将
currentImage
currentLabel
链接到其情节提要对象?再次检查图像名称,是否匹配?是否可以共享您的整个DetailViewController类?是否看起来不像这样?类PhotoDisplayViewController:UIViewController{…?在PhotoDisplayViewController类中,我建议向didViewLoad()添加类似的内容,以确保传递对象。如果让myPhoto=currentPhoto{..@Esric Qian:图像名称匹配正确。我试图检查是否匹配,控制台输出是否在那里。此外,文本字段也没有显示在那里:(代码看起来很好。您是否检查了
PhotoDiplayViewController
中的
image
是否为非零,并且已正确加载?您是否将
currentImage
currentLabel
链接到了它们的故事板对象?我正在考虑您提到的解决方案。您提到不要使用Segue,但我不知道具体方法。)ason。你能解释一下避免segue的原因吗?这就像一个全局变量。我不推荐这种方法。我推荐使用这种方法,因为它更容易管理和创建。使用segue只是为了传递一点数据,这是一个复杂且不必要的步骤。我正在考虑你提到的解决方案。你提到不要使用Segue,但我不知道原因。你能解释一下避免segue的原因吗?这就像一个全局变量。我不建议使用这种方法。我建议使用这种方法,因为它更易于管理和创建。使用segue是一个复杂且不必要的步骤,只是为了传递一点数据。这不是一个bug,这是已安装的属性y,即大小类相关。EX Image View仅安装在wRegular hAny上。这不是错误,这是已安装的属性,即大小类相关。EX Image View仅安装在wRegular hAny上。