英文:
UIkit ChildController Retain Cycle
问题
你好,以下是您要翻译的内容:
"Hi I am making an app where I use a tableView like Apples Settings app. I use this tableview in various controllers, so I made a controller that is added as a child to the controllers that require the use of this tableview.
The reason it is a child controller is because it needed the layout and functionality of the table to be used repeatedly in multiple controllers.
The problem I'm having is adding a child controller keeps the controller retained in memory.
Here's how to remove the child before the main controller disappears:
guard parent != nil else { return }
willMove(toParent: nil)
removeFromParent()
view.removeFromSuperview()
This is how I add the child to the main controller:
addChild(controller)
controller.view.frame = self.view.frame
view.addSubview(controller.view)
controller.didMove(toParent: self)
Child controller:
class ChildController: UIViewController {
//MARK: - Propeties
public var sections: [FormSection_Model] = [] {
didSet {
tableView.reloadData()
}
}
public private(set) lazy var tableView: UITableView = {
return UITableView(frame: .zero, style: style)
}()
override func setUp() {
super.setUp()
hideKeyboardWhenTappedAround()
tableView.dataSource = self
tableView.delegate = self
tableView.register(ButtonForm_Cell.self)
tableView.register(SwitchForm_Cell.self)
tableView.register(TextFieldForm_Cell.self)
tableView.register(TextViewForm_Cell.self)
tableView.register(PickerForm_Cell.self)
tableView.register(MultiPickerForm_Cell.self)
tableView.register(LinkForm_Cell.self)
tableView.contentInsetAdjustmentBehavior = .never
}
override func setUpLayout() {
super.setUpLayout()
view.addSubview(tableView)
tableView.frame = view.frame
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 150, right: 0)
}
}
extension ChilsController: UITableViewDelegate, UITableViewDataSource {
//TableView delegate funcs...
}
Main Controller:
class Controller: UIViewController {
public private(set) lazy var formController: ChildController = {
let controller = FormTableView_Controller(style: .insetGrouped)
return controller
}()
override func setUp() {
super.setUp()
addChildController(formController)
}
override func cleanContent() {
super.cleanContent()
formController.removeFromParentViewController()
}
private func setSections() {
formController.sections.append([
//add items to child controller
])
}
}
希望对您有所帮助!
英文:
Hi I am making an app where I use a tableView like Apples Settings app. I use this tableview in various controllers, so I made a controller that is added as a child to the controllers that require the use of this tableview.
The reason it is a child controller is because it needed the layout and functionality of the table to be used repeatedly in multiple controllers.
The problem I'm having is adding a child controller keeps the controller retained in memory.
Here's how to remove the child before the main controller disappears:
guard parent != nil else { return }
willMove(toParent: nil)
removeFromParent()
view.removeFromSuperview()
This is how I add the child to the main controller:
addChild(controller)
controller.view.frame = self.view.frame
view.addSubview(controller.view)
controller.didMove(toParent: self)
Child controller:
class ChildController: UIViewController {
//MARK: - Propeties
public var sections: [FormSection_Model] = [] {
didSet {
tableView.reloadData()
}
}
public private(set) lazy var tableView: UITableView = {
return UITableView(frame: .zero, style: style)
}()
override func setUp() {
super.setUp()
hideKeyboardWhenTappedAround()
tableView.dataSource = self
tableView.delegate = self
tableView.register(ButtonForm_Cell.self)
tableView.register(SwitchForm_Cell.self)
tableView.register(TextFieldForm_Cell.self)
tableView.register(TextViewForm_Cell.self)
tableView.register(PickerForm_Cell.self)
tableView.register(MultiPickerForm_Cell.self)
tableView.register(LinkForm_Cell.self)
tableView.contentInsetAdjustmentBehavior = .never
}
override func setUpLayout() {
super.setUpLayout()
view.addSubview(tableView)
tableView.frame = view.frame
tableView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 150, right: 0)
}
}
extension ChilsController: UITableViewDelegate, UITableViewDataSource {
//TableView delegate funcs...
}
Main Controller:
class Controller: UIViewController {
public private(set) lazy var formController: ChildController = {
let controller = FormTableView_Controller(style: .insetGrouped)
return controller
}()
override func setUp() {
super.setUp()
addChildController(formController)
}
override func cleanContent() {
super.cleanContent()
formController.removeFromParentViewController()
}
private func setSections() {
formController.sections.append([
//add items to child controller
])
}
}
答案1
得分: 0
以下是翻译好的部分:
"Delete that. Neither parent nor child must have a stored reference to the other. If they need to speak of one another at all, the child must speak only of the parent view controller and the parent must speak only of its children, using the parent
and children
references already provided in the UIViewController class."
不要有别的内容。
英文:
Here's the problem:
public private(set) lazy var formController: ChildController = {
let controller = FormTableView_Controller(style: .insetGrouped)
return controller
}()
Delete that. Neither parent nor child must have a stored reference to the other. If they need to speak of one another at all, the child must speak only of the parent view controller and the parent must speak only of its children, using the parent
and children
references already provided in the UIViewController class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论