Lets say I have 3 Files/Classes:
File A (Main View):
File B (functions to Initialize Additional Views):
File C (functions that update the data/labels in the views called from File B into File A):
File A is the main view. I use a generic function from File B called "initHeader()" that initializes a "header view" and adds it to the view in File A. The reason the view is initialized from File B is because there is File A 1 to 50. I didnt want to copy paste the code to initialize the the new view in every version of File A.
After the header is initialized, I will want to update some data in this view. How can I update the data in this view if
1) there is no variable assigned to the new view in File A initialized from File B. I realize I could loop through all subviews in File A to see if a view matches the type of view initialized fomr File B, but I want to update it without having a pointer pointing to the new "header view" variable. Which brings me to
2) What if I want to update the File B view that was added to File A through File C.
In this case:
->File A calls a function from File B to initialize the "header view"
->I want the newly added "header view" to be update with different data (this method will be called from File C so I don't need to have the same function a million times in all the different sub File A files.
If I were to do it all in just one file/class. I could easily do it:
->var headerView = HeaderView()
->headerView.updateAllVariables(fromDataObject: dataObject)
BUT if I wanted to initialize the header view from approximately 50 different classes, I would need to copy and paste the same code to each class. Then if I want to change something it becomes a HUGE headache.
How can I call all those from different files so I literally just write:
->loadHeaderView (from File B)
->updateHeaderView(fromData: dataObject) (from File C)
Hope that makes sense! Do I make the functions in file B return the view after being initialized? Which would make it look something like:
File A:
var headerView = initializeHeaderView() <- From File B
updateView(headerView: headerView, fromData: dataObject) <- From File C
Do you have 50 different classes or 50 instances of object of the same class ?
If they are 50 instances of the same class, (that is what I understand) you will have the code once.
However, if they are of different class, you have to replicate in each class… unless you decide to create an intemediate subclass where you implement the function and have all 50 classes inherent from this intermediate class.
Here is the structure
class ViewForDelegation : UIView {
// In a didLoad method
NotificationCenter.default.addObserver(self, selector: #selector(notification1Action), name:NSNotification.Name(rawValue: notification1Name), object: nil) // object is nil: we don't test you sent the notification ; but we could give an object reference if we wanted to test
func notification1Action() {
propertyInA = //
// Can be a textField content as well
textFieldInA.text = "I updated"
}
}
// Then the 50 classes
class ViewNumber1 : ViewForDelegation {
}