问题描述
我有一个 ViewController,它使用多个子视图(HomeViewController 等),可以通过我的应用程序底部的自定义选项卡栏进行选择。在 HomeViewController 内部有一个 UIView,其中包含一个 UITableView,其中包含一个带有名称和图像的原型自定义单元。
import UIKit
class HomeViewController: UIViewController {
@IBOutlet weak var friendView: UITableView!
let friends = ["batman","harsh","ava","sasha","fatima","alfred"]
override func viewDidLoad() {
super.viewDidLoad()
friendView.delegate = self
friendView.dataSource = self
friendView.allowsSelection = false
}
}
extension HomeViewController: UITableViewDelegate,UITableViewDataSource {
func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
return 120
}
func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
return friends.count
}
func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = friendView.dequeueReusableCell(withIdentifier: "customCell") as! CustomCell
let friend = friends[indexPath.row]
cell.avatarImg.image = UIImage(named: friend)
cell.nameLbl.text = friend
return cell
}
}
自定义单元格:
import UIKit
class CustomCell: UITableViewCell {
@IBOutlet weak var friendView: UIView!
@IBOutlet weak var nameLbl: UILabel!
@IBOutlet weak var avatarImg: UIImageView!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool,animated: Bool) {
super.setSelected(selected,animated: animated)
// Configure the view for the selected state
}
}
当我启动应用程序时,一切看起来都很好。但是,当我开始在表格内滚动时,所有数据突然消失了。故事板和代码之间的所有关系都应该很好。我认为这可能与我需要使用子视图有关。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var tabBarView: UIView!
@IBOutlet weak var contentView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
Design.makeCornersRound(view: tabBarView,radius: 10.0)
Timer.scheduledTimer(withTimeInterval: 0.1,repeats: false) { (timer) in
self.switchToHomeViewController()
}
}
@IBAction func onClickTabBar(_ sender: UIButton) {
let tag = sender.tag
if tag == 1 {
switchToIncomingsViewController()
}
else if tag == 2 {
switchToSpendingsViewController()
}
else if tag == 3 {
switchToHomeViewController()
}
else if tag == 4 {
switchToSavingsViewController()
}
else if tag == 5 {
switchToSettingsViewController()
}
}
func switchToHomeViewController() {
guard let Home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
contentView.addSubview(Home.view)
Home.didMove(toParent: self)
}
...
}
参考我一直在尝试实施的教程:https://www.youtube.com/watch?v=ON3Z0PXSoVk
解决方法
在这个函数中:
func switchToHomeViewController() {
// 1
guard let Home = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
// 2
contentView.addSubview(Home.view)
// 3
Home.didMove(toParent: self)
// 4
}
- 在
1
,您创建HomeViewController
的实例 - 在
2
,您将其视图添加到cotentView
- 在
3
,您调用didMove()
... 但这没有任何作用,因为您尚未将控制器添加到您的层次结构中 - 在
4
,您的Home
实例消失了,因此该控制器中的代码不再存在
您需要将控制器添加为子控制器。
作为旁注,变量名使用小写:
func switchToHomeViewController() {
// create an instance of HomeViewController
guard let homeVC = self.storyboard?.instantiateViewController(withIdentifier: "HomeViewController") as? HomeViewController else { return }
// add it as a child view controller
self.addChild(homeVC)
// add its view
contentView.addSubview(homeVC.view)
// here you should either set the view's frame or add constraints
// such as:
homeVC.view.frame = contentView.bounds
// inform the controller that it moved to a parent controller
homeVC.didMove(toParent: self)
}