Update #IBOutlets via model - ios

I'm trying to decouple my outlets from the controller in order to migrate the view logic into a view model. Ideally I would like to be able to call an updateUI method from within the controller and have it filter down to the labels and text fields within the storyboard.
How I envisioned it working would be as follows:
Inside my controller:
var updateUI: ViewWithFieldsInside {
return ViewWithFieldsInside.reloadData(newData)
}
Inside my view I would have all the outlets hooked in via a UIView class (linked to from storyboard) and have a reloadData method that would set the values equal to newData
Is this the correct way to handle this via Storyboard implementation or am I missing something?

Related

Set ViewController as Delegate of another UIView Class

Lets say I have 2 classes.
One is the well known ViewController (that is set to be the custom class of the first scene in the Interface Builder).
And another class, lets call it B, that I created to draw something inside another view that is a sub view of the main view.
Since I have to get notified whenever some values changes in the sub view in order to do some things in the view controller, I decided to use the delegate pattern.
So I set up the protocol in the B class, but now I have to link the delegate to the view controller class. How can I manage to get the view controller class from the class B?
By the way I instantiate some things using override function willMove(toSuperview newSuperview: UIView?) in B. Can I maybe use newSuperView to get the view controller?
I know it is not the best approach since it may break the MVC Pattern, but I don't know which better way to do this.
To get an idea of what I mean you can look at the image.

Accessing View Objects In Container View

I have a UIViewController (1).
Within this UIViewController I have a Container View (2).
In the Container View's (2) UIViewController (3) I have a UILabel.
How do I access the label text property from The first UIViewController(1)?
My UIStoryboard looks like this:
What you likely want to do is create a reference to the child view controller in the parent view controller, and access the label property through the child view controller.
Generally, however, you don't want to have a parent object directly control a child object's elements. In essence, you want the responsibility of managing the UILabel, and its contents, to fall on whichever view controller owns it. So I would recommend designing the app logic in such a way that the child view controller totally manages its own objects.
For example, instead of simply giving public access to the UILabel on your child view controller, you could create a method like - (void)UpdateYourLabelWithText:(NSString *)text; that would then make updates internally. This separates your view control logic and delegates responsibility to the correct view controller.
Implement prepareForSegue. It will be called when view controller (1) is instantiated. You can assign the segue.destinationViewController (which is view controller (3)) to a property at that time, and then you can access any of its properties.

How to create a base ViewController class that has a UITableView?

I have several View Controllers, all using a UITableView, with identical custom cells. The UITableView Datasource methods are identical too (to the line); all that changes is the data source itself (the array from which the table view is loaded). It seems rather redundant for me to have to copy the exact same code for 5 such View Controllers (not to mention it's a bad coding practice!).
I therefore thought of creating a base (or parent) view controller that extends UIViewController, and have all my following (child) controllers inherit from this base view controller.
My problem is this: My base view controller needs to have a UITableView property, and it also contains the DataSource methods. I cannot get the UITableViews within the child view controllers to refer to the parent class for their DataSource.
Am I on a completely wrong track? Is there a better approach to this problem? Any help is greatly appreciated!
Just have one controller into which you add a property for the data source.
When you segue to the controller or create it set the data source property.
If all the code is identical except the data source array, you only need one controller.
In xcode add a new source file: e.g. MyDataViewerController which inherits UITableViewController.
In that file when created, add a property of the form:
#property NSArray *dataSource;
Click on one of your view controllers in storyboard and click the Class attribute inspector tab. Set the class to MyDataViewerController.
You can now use this controller to show any of your data. You can probably delete all the others.
I've assumed its an array containing the data. Replace this with whatever you use.
Assuming you segue to this controller from somewhere, set the property in the
prepareForSegue function.

move tableview delegates to different class

I have one view controller containing lots of operations. Most of the views are created dynamically. To separate out code I created a class as a myClass of NSObject where I created a function showMyTableView. This function adds a tableview as subview. but I have to write all tableview delegates inside view controller. How I can move require delegates to myclass?
Tomorrow I may require to add subview containing buttons for which I should write action in separate class.
How I can accomplish same?
your new class should handle data only, not ui.
for different table cellview consider use separate tableviewcell subclasses and provide them data models. let them configure themselves
(By 'table view delegates', I assume you mean your table view's delegate and dataSource?)
You can certainly move this stuff to another class. You would simply set the delegate/dataSource of your table view to an instance of MyClass, in your view controller, like so:
self.tableView.delegate = instanceOfMyClass;
MyClass probably shouldn't be responsible for creating the table view and adding it to your view controller's view though. Your view controller should be responsible for this itself.

How to use a UIView delegate in initialization stage

I have a view controller which has a couple of views. Those views need data from the model and I use the view controller as a delegate to supply them that data.
The problem is that they need some of that data when they are initialized (in initWithFrame/awakeFromNib). At that stage the delegate is not set yet (it is set in the view controller's viewDidLoad which is called after the view is initialized).
I can solve it by just accessing the model directly from the view, but that will create quite a mess if every view would access models directly.
Where can I set the delegate in order to use it in the view's awakeFromNib/init?

Resources