Reputation: 2443
I was looking at a tutorial by Brian Voong at https://youtu.be/hexuJ6qL06I?t=1678
There is this part of the code with this line header.viewController = self.
I have difficulty understand this particular line here. Is there anyone who could explain what is meant by header.viewController = self? Is it that it is for putting the viewController in header but what does that mean? Would appreciate it if someone could provide me with some explanation. I even have difficulty finding this info using google. I simply do not know what keywords to use for searching.
override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: "headerId", forIndexPath: indexPath) as! TaskHeader
header.viewContoller = self
return header
}
Upvotes: 1
Views: 1306
Reputation: 944
header.viewContoller = self
'viewController' is just a property of header, and the code makes this property pointer to self, self is the current controller.
Then in your header, you can use:
self.viewController.view.backgroundColor = UIColor.orangeColor
And the background of current controller will be changed.
Upvotes: 1
Reputation: 14477
header
is an object of separate class(TaskHeader) which has an optional property viewController
. And when user tap on header's Add button it invoke a method on TaskHeader
class addTask
(headerView) which at the end invokes a viewController.addNewTask method.
So he is keeping this so we can easily get viewController reference by which he can invoke method on viewController class from Taskheader Class. You can look in to TaskHeader class to understand how it works.
class TaskHeader: BaseCell {
var viewController: ViewController?
let taskNameTextField: UITextField = {
let textField = UITextField()
textField.placeholder = "Enter Task Name"
textField.translatesAutoresizingMaskIntoConstraints = false
textField.borderStyle = .RoundedRect
return textField
}()
let addTaskButton: UIButton = {
let button = UIButton(type: .System)
button.setTitle("Add Task", forState: .Normal)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
override func setupViews() {
addSubview(taskNameTextField)
addSubview(addTaskButton)
addTaskButton.addTarget(self, action: "addTask", forControlEvents: .TouchUpInside)
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-8-[v0]-[v1(80)]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": taskNameTextField, "v1": addTaskButton]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-24-[v0]-24-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": taskNameTextField]))
addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-8-[v0]-8-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": addTaskButton]))
}
func addTask() {
viewController?.addNewTask(taskNameTextField.text!)
taskNameTextField.text = ""
}
}
Upvotes: 1