【发布时间】: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
}
}
}
【问题讨论】:
-
你能分享一个演示吗
-
你能查一下吗? xib 的文件所有者正确设置为 AboutUsViewController
-
@ManuRaphy 设置正确。当我遵循 Sh_Khan 的回答时,这个问题得到了解决。谢谢!
标签: swift xcode uiviewcontroller xib subview