Intercepting didAddSubview - ios

I hope this is a simple question. I need to intercept didAddSubview, but will I need to subclass the UIView in order to override that method?
The UIView that I want to override is the UIViewController's view property, so I just want to know how to go about working with this.
Thanks!

From Apple UIView documentation (see Methods to Override):
When subclassing UIView, there are only a handful of methods you
should override and many methods that you might override depending on
your needs. Because UIView is a highly configurable class, there are
also many ways to implement sophisticated view behaviors without
overriding custom methods, which are discussed in the Alternatives to
Subclassing section. In the meantime, the following list includes
the methods you might consider overriding in your UIView subclasses:
...
didAddSubview:, willRemoveSubview: - Implement these methods as needed to track the additions and removals of subviews.
...
So, create your UIView subclass and override the method. Then, say to your UIViewController that its view will be the one you have subclassed. To achieve this there are a couple of ways:
by Xib
implementing loadView method and set the view property to be your custom view
redifining the view property in viewDidLoad
Hope that helps.

Related

Need clarification regarding MVC design pattern

I have a reusable UIView with its own .xib file. This UIView would be added to different UIViewControllers as subviews. In the MVC design pattern, my reusable UIView should contain only code for the user interface(UILabels, UIButons, etc..). What I'm confused about is setting up the UILabels and UIButtons. Is the ViewController that contains my UIView responsible for setting up my UIView's UIButton click events and what my UILabel displays? The problem I'm having now is that I have multiple UIViewControllers that reuse the same UIView, but they all implement the same code that sets up my UIView. This end up with lots of duplicate code across my UIViewControllers. If I move the setup code to my UIView to reduce the duplicate code, doesn't that violate the MVC design pattern? Is there a way to create a "Controller" thats only responsible for setting up my UIView so I could reuse my UIView and Controller inside my UIViewControllers?
Assuming that by setup you mean instructing the UIView on how to draw itself based on some object, this is exactly what the drawRect method is for. UIView also provides an initWithFrame method and you can even create your own initializer if you want. One more option is to create a function in your UIView subclass that will take what ever info you are displaying and will setup the view way you want. Something like: setupCustomViewWithMyObject(object).
All this to say that your setup code should be in your UIView subclass and you are not violating anything.

Collectionview Controller vs Collectionview

I'm beginner at IOS. I don't quite understand what is difference between collection view and collecionviewcontroller and in what cases can I use each of them
UICollectionView inherits from UIScrollView (just another UIView)
UICollectionViewController inherits from UIViewController..
and it implements some protocols.. like UICollectionViewDelegate and UICollectionViewDataSource .. it means everything is already done for you.. and you just have to use it.. but as everything is already done for you.. you may not be able to do some stuff.. like resizing your collectionView ..
if you want full control I recommend you to use your own UIViewController.. and add a UICollectionView as a subview.. so you can place it wherever you want and resize it.. don't forget to implement UICollectionView protocols for delegation and datasource :)
It's based on your requirements. You may change some properties with your View controllers. But when you use as part of its' controllers, you can't change it.
For example you CanĀ“t change UICollectionView Size of UICollectionViewController.
In these case, you must use UICollectionView in viewcontroller. As like this, if you want to customize some property but functionality are same in all.
Here I mention some ref for you: refrence
UICollectionViewController is specialized in managing UICollectionView. Collection view controller is inherited from UIViewController specifically for collection views so if collection views are removed then there is no use for Collection view controller.
Having said that,collection views can be used inside UIViewController whereas UIView can't be added inside UICollectionViewController.

Can a property of a UITableView be the UITableView Delegate and DataSource

I have a custom view that's a bit of a hack. Basically it's a UIView with tableView as it's property, with additional views in the tableView that need their own delegates. I need a viewController for the UITableView and can't make the UIView it's delegate according to this SO link Custom UIView as UITableView delegate and datasource?.
Can I make the UITableView have a property of UIViewController and set that UIViewController as the tableView's delegate?
In this case according to OOP, the UITableView has a UIViewController so technically, I could expect this to work. But, I am wondering if down the line somewhere this could create problems since the UITableView and UIViewController are coupled in this way.
You don't need a UIViewController for the UITableView - you just need an object or objects that implement the data source and a delegate protocols. As per the accepted answer on the question you linked to you can use a separate controller class to provide this.
The right answer depends a little on how the table is used with your UIView subclass.
If the table will always have the same content (Say a list of months) and there is no value in exposing or abstracting the properties then you can code the delegate and dataSource inside your UIView subclass or in an assistant class.
If the table content will vary depending on how the UIView is used (say a list of people where you don't know what the list is - friends, relatives, employees...) then it would make sense to simply expose the tableview's datasource (and delegate if necessary) properties via your UIView subclass

Reusing implementation of UITextField Delegate methods

In my application I want to use a UITextField multiple times. And for each of this UITextField I want to implement the UITextFieldDelegate methods (didEndEditing, didBeginEditing, should...).
My idea was to subclass the UITextField and set the delegate to itself. Then implementing the delegate methods as I want them to be implemented. It this case I would be able to use the subclassed UITextField anywhere, and the delegate methods will be implemented once, so I won't have to implement them in every VC separately.
BUT!
Delegation is used the communicate to another object to delegate some stuff to that object. It makes no sense to set the delegate to itself.
How should I implement the delegate methods ONCE so every time I place the UITextField using Interface Builder and set the class to the custom UITextField, the textfield will behave according tot the implemented delegate methods.
Thanks for all your help
My solution might not be optimal and I'm still seeking for a better one, but has no one is answering you, here we go :
A UIView should never try to "control" anything : it's just a view. That's why through my projects, all my UIViewController inherit from a base UIViewController (BaseViewController). This BaseViewController has all the UITextField delegation code, so whenever a UIViewController set a UITextField delegate to self, the BaseViewController handles it. Because I want my BaseViewController to be able to control all my view's UIScrollView content offset, I also have a base class BaseView which all my UIViews are inheriting, that is having by default a UIScrollview.

Subclass UITableView with Custom UITableViewCells

What I have: 10+ view controllers using a UITableView with custom UITableViewCell. Each view controllers load different data and forward to different sub-pages, thus they cannot combine together.
The aim of the change is to centralize the management of table view's look & feel in a single custom UITableView subclass. But since the data loaded by each view controller is different, I think the UITableViewDataSource and UITableViewDelegate have to be assigned to its original view controller class.
How do I make the change? Or I am thinking at wrong direction?
A tableview's datasource can be separate and independent from its delegate. So, put all of your appearance configurations in a UITableView subclass that implements its own delegate methods, and then create a separate NSObject subclass for each tableview that is responsible for the datasource implementation.
You could make a superclass for all your view controllers that collects all the common logic.

Resources