【问题标题】:Subviews not recognized in UIViewController connected to xib file在连接到 xib 文件的 UIViewController 中无法识别子视图
【发布时间】:2018-12-30 18:00:39
【问题描述】:

我有一个连接到 xib 文件的 UIViewController swift 文件(我在创建 viewController 时单击了创建 xib)。现在,它无法识别主视图中的任何子视图。

当我在调试时写“po self.view.subviews”。它返回 0。所有子视图(tableView 和 textView 和标签)= nil。我什至创建了一个新项目,但仍然遇到同样的错误。 我在这里不知所措。

这是来自 AboutUsViewController swift 文件的代码:

导入 UIKit

导入基础

类 AboutUsViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

@IBOutlet weak var textView: UITextView! 

@IBOutlet weak var contactsTableView: UITableView!

let items = ["Item 1", "Item2", "Item3", "Item4"]


override func viewDidLoad() {
    super.viewDidLoad()
    let NibName = UINib(nibName: "ContactUsTableViewCell", bundle: nil)
    contactsTableView.register(NibName, forCellReuseIdentifier: "ContactUsTableViewCell")

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

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


/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
}
*/
// MARK: - Table view data source

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return items.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if (indexPath.item == 0) {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactUsTableViewCell", for: indexPath) as! ContactUsTableViewCell
        cell.label.text = "HIII"
        return cell

    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: "ContactUsTableViewCell", for: indexPath) as! ContactUsTableViewCell
        cell.label.text = items[indexPath.item]
        return cell
    }
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if (indexPath.item == 0) {
        return 200
    } else {
        return 86
    }
}

}

my swift file

my xib file

【问题讨论】:

  • 你能分享一个演示吗
  • 你能查一下吗? xib 的文件所有者正确设置为 AboutUsViewController
  • @ManuRaphy 设置正确。当我遵循 Sh_Khan 的回答时,这个问题得到了解决。谢谢!

标签: swift xcode uiviewcontroller xib subview


【解决方案1】:

确保像这样加载vc

let vc = AboutUsViewController(nibName: "AboutUsViewController", bundle: nil)
self.window?.rootViewController = vc
self.window?.makeKeyAndVisible()

【讨论】:

  • 非常感谢!我已经尝试解决这个问题两个小时了。你知道为什么我的旧代码不起作用吗?在应用程序委托中的 func 应用程序下,我有: if self.window == nil { window = UIWindow(frame: UIScreen.main.bounds) window?.rootViewController = AboutUsViewController() window?.backgroundColor = UIColor.white window?. makeKeyAndVisible() }
  • 现在我有:让 viewController = AboutUsViewController(nibName: "AboutUsViewController", bundle: nil) if self.window == nil { window = UIWindow(frame: UIScreen.main.bounds) 窗口? rootViewController = viewController window?.backgroundColor = UIColor.white window?.makeKeyAndVisible() }
猜你喜欢
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
  • 2016-11-25
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2012-04-23
  • 1970-01-01
相关资源
最近更新 更多