Swift segue之后在视图控制器中看不到情节提要元素

Swift segue之后在视图控制器中看不到情节提要元素,swift,xcode,Swift,Xcode,我在我的故事板中添加了一个带有一些简单表单元素的视图控制器(vc2),并从一个现有的tableviewcontroller(vc1)拖动了一段ctrl键,该控件由一个拖尾滑动按钮触发 我可以在调试器中看到打印函数的输出(在下面的vc2代码中),但我的表单元素不可见。出于某种原因,我不得不手动设置背景颜色,默认为黑色,而不是故事板上设置的颜色。我认为这与我加载vc2的方式有关,但我试图将代码更改为正常的performsguewithidentifier导致了错误 “NSInvalidArgumen

我在我的故事板中添加了一个带有一些简单表单元素的视图控制器(vc2),并从一个现有的tableviewcontroller(vc1)拖动了一段ctrl键,该控件由一个拖尾滑动按钮触发

我可以在调试器中看到打印函数的输出(在下面的vc2代码中),但我的表单元素不可见。出于某种原因,我不得不手动设置背景颜色,默认为黑色,而不是故事板上设置的颜色。我认为这与我加载vc2的方式有关,但我试图将代码更改为正常的performsguewithidentifier导致了错误

“NSInvalidArgumentException”,原因:“Receiver()没有与 标识符

然后我删除并重新制作了这个片段,但没有效果。所以我把代码改回这个,它可以工作,但不能呈现故事板元素

func clickView(forRowAtIndexPath indexPath: IndexPath) {
    let myContents = UnitComponents[indexPath.row]
    print("Clicked Report \(self.ProjectID) \(self.ShipListID) \(self.UnitName) \(myContents.sku)")

    self.Sku = myContents.sku

    let vc = ComponentViewController()
    vc.Sku = myContents.sku
    navigationController?.pushViewController(vc, animated: true)        

}
这是vc2代码

import UIKit

class ComponentViewController: UIViewController {

var Sku = ""
var UnitName = ""
var ShipListID = ""
var ProjectID = ""

@IBOutlet var damageDesc: UITextView!

@IBOutlet var repairCost: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()

    self.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)

    print("Hello World \(Sku) \(UnitName) \(ShipListID) \(ProjectID)");

    // Do any additional setup after loading the view.
}

@IBAction func saveReport(_ sender: Any) {

    print("damageDesc \(String(describing: damageDesc ?? nil))")
    print("repairCost \(String(describing: repairCost ?? nil))")


}



}
如何修复代码,以便加载故事板布局等,并在应用程序中查看表单元素

这是转到其他视图控制器的错误方式吗?我这样问是因为围绕这个主题的一些问题似乎表明它没有正确地调用故事板。我正在搜索一个swift 5示例,该示例说明如何执行此操作,并且只查找对InstanceEviewController WithiIdentifier(“ViewController”)的引用,该引用似乎不在swift 5中

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let vc = ShippingUnitsTableViewController()
    vc.ShipListID = ShipLists[indexPath.row].ListID
    vc.ProjectID = ProjectID
    navigationController?.pushViewController(vc, animated: true)

}
你能试试吗

func clickView(forRowAtIndexPath indexPath: IndexPath)
取而代之的是,您选择了tableview委托方法行或view controller的PerformsgueWithIdentifier?

您能试试吗

func clickView(forRowAtIndexPath indexPath: IndexPath)

相反,我选择了tableview委托方法行或view controller的PerformsgueWithIdentifier?

确定此问题的解决方案需要更改我在视图控制器之间移动的方式。下面的didSelectRowAt indexPath方法不使用情节提要,这会导致目标视图控制器有点孤立,并且不知道情节提要的顺序

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let vc = ShippingUnitsTableViewController()
    vc.ShipListID = ShipLists[indexPath.row].ListID
    vc.ProjectID = ProjectID
    navigationController?.pushViewController(vc, animated: true)

}
我将didSelectRowAt indexPath更改为此,并向prepareSegue方法添加了一些必要的信息,如下所示

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.ListID = ShipLists[indexPath.row].ListID
    performSegue(withIdentifier: "UnitsVC", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "UnitsVC"{
        let nextViewController = segue.destination as? ShippingUnitsTableViewController
        nextViewController!.ProjectID = self.ProjectID
        nextViewController!.ShipListID = self.ListID
    }
}

好的,这个问题的解决方案需要改变我在视图控制器之间移动的方式。下面的didSelectRowAt indexPath方法不使用情节提要,这会导致目标视图控制器有点孤立,并且不知道情节提要的顺序

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let vc = ShippingUnitsTableViewController()
    vc.ShipListID = ShipLists[indexPath.row].ListID
    vc.ProjectID = ProjectID
    navigationController?.pushViewController(vc, animated: true)

}
我将didSelectRowAt indexPath更改为此,并向prepareSegue方法添加了一些必要的信息,如下所示

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    self.ListID = ShipLists[indexPath.row].ListID
    performSegue(withIdentifier: "UnitsVC", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "UnitsVC"{
        let nextViewController = segue.destination as? ShippingUnitsTableViewController
        nextViewController!.ProjectID = self.ProjectID
        nextViewController!.ShipListID = self.ListID
    }
}

您需要指定viewcontroller所在的情节提要,然后从该情节提要实例化viewcontroller。将您的func替换为以下func:-

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)    
    let vc = storyboard.instantiateViewController(withIdentifier: "ShippingUnitsTableViewController") as! ShippingUnitsTableViewController
    vc.ShipListID = ShipLists[indexPath.row].ListID
    vc.ProjectID = ProjectID
    navigationController?.pushViewController(vc, animated: true)
}
并确保(在Main.storyboard文件中)在属性检查器中为ViewController(需要显示)的序列图像板ID提供了ShippingUnitsTableViewController(请参见下图):-


您需要指定viewcontroller所在的情节提要,然后从该情节提要实例化viewcontroller。将您的func替换为以下func:-

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: nil)    
    let vc = storyboard.instantiateViewController(withIdentifier: "ShippingUnitsTableViewController") as! ShippingUnitsTableViewController
    vc.ShipListID = ShipLists[indexPath.row].ListID
    vc.ProjectID = ProjectID
    navigationController?.pushViewController(vc, animated: true)
}
并确保(在Main.storyboard文件中)在属性检查器中为ViewController(需要显示)的序列图像板ID提供了ShippingUnitsTableViewController(请参见下图):-



不太确定你的建议是什么。如果我将navigationController?.pushViewController(vc,动画:true)更改为performSegue(标识符为:“ReportFormVC”,发件人:self),我会收到一个错误-由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“Receiver()没有标识符为“ReportFormVC”的segue”删除您的clickView函数,使用tableview的didSelectRow方法,也可以使用与视图控制器具有相同用途的PerformsgueWithIdentifier方法。因为您没有为viewcontroller设置任何标识符,所以您应该转到storyboard ReportFormVC,然后从xcode的右面板中选择“显示属性检查器”,指定segue标识符。segue已经在属性检查器中设置了正确的标识符。不确定您的建议。如果我将navigationController?.pushViewController(vc,动画:true)更改为performSegue(标识符为:“ReportFormVC”,发件人:self),我会收到一个错误-由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“Receiver()没有标识符为“ReportFormVC”的segue”删除您的clickView函数,使用tableview的didSelectRow方法,也可以使用与视图控制器具有相同用途的PerformsgueWithIdentifier方法。因为您没有为viewcontroller设置任何标识符,所以您应该转到storyboard ReportFormVC,然后从xcode的右面板中选择“显示属性检查器”,指定segue标识符。segue已在属性检查器中设置了正确的标识符。此行错误:
让vc=ComponentViewController()
请参阅此行错误:
让vc=ComponentViewController()
请参阅谢谢。我用segues发布的答案也很有效。您的解决方案是更好的方法吗?如果是这样的话,我会把你的答案标记为正确。我刚才对同一件事说了不同的方法,如果你不想声明'prepare'func,你可以使用这个方法。最后,使用最适合你的。经过测试,效果很好。再次感谢您提供此替代方案approach@MikeVolmar你介意把答案也投上一票吗?我几天前投了一票。我也会把它标为正确答案。我用segues发布的答案也很有效。您的解决方案是更好的方法吗?如果是这样的话,我会把你的答案标记为正确。我刚才对同一件事说了不同的方法,如果你不想声明'prepare'func,你可以使用这个方法。最后,使用最适合你的。经过测试,效果很好。再次感谢您提供此替代方案approach@MikeVolmar你介意把答案也投上一票吗?我几天前投了一票。我也会把它标为正确答案