swift - 在 segue 之后, Storyboard元素在 View Controller 中不可见

标签 swift xcode

我将带有一些简单表单元素的 View Controller (vc2) 添加到我的 Storyboard 中,并从现有的 tableviewcontroller (vc1) 进行了 segue ctrl 拖动,这是由拖尾滑动按钮触发的

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

'NSInvalidArgumentException', reason: 'Receiver () has no segue with identifier

然后我删除并重新制作了 segue,但没有任何效果。所以我将代码改回了这个,它可以工作但不渲染 Storyboard 元素。

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))")


}



}

如何修复我的代码,以便加载 Storyboard布局等,并且我可以在应用程序中看到表单元素?

这是转到另一个 View Controller 的错误方法吗?我问是因为似乎围绕这个主题的一些 SO 问题表明它没有正确调用 Storyboard 。我正在搜索如何执行此操作的 swift 5 示例,并且只找到对 instantiateViewControllerWithIdentifier("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)

}

最佳答案

你需要指定viewcontroller所在的storyboard,然后从那个storyboard实例化viewcontroller。将您的功能替换为以下功能:-

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 的(需要显示的) Storyboard ID 设为 ShippingUnitsTableViewController(见下图):-

enter image description here

关于swift - 在 segue 之后, Storyboard元素在 View Controller 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57279810/

相关文章:

objective-c - 如何使 Swift 类在 XPC 服务中可用?

ios - playground.xcworkspace 无法保存,因为文件已存在

ios - 在 Objective-C 中将一个字符串切成另外两个

iphone - Facebook iOS SDK 3.0 教程中的 SIGABRT 错误

ios - 选择图像后如何执行 Segue

ios - 为什么我对 array.sorted( 的实现是 : ) in Swift returning a strange order?

swift - 如何在一天中的不同时间创建通知?

ios - swift 3 从 textField 中删除大量文本

iphone - 检测无线网络名称

xcode - 带圆角的 NSVisualEffectView