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.
Related
I have dragged out a splitViewController, and identified it as a subclass I have created MySplitViewController.
When Right-clicking the splitview storyboard I can see that I've set its Master and Detail view controllers, and furthermore that the delegate is NOT set.
I have made my subclass conform to the protocol and implemented some methods, but they are not being called (which I not understand is because the delegate is not set).
But whenever I try to ctrl+drag from the delegate option in the storyboard to my class, it won't link up. In fact, it won't link up with anything. Am I using this protocol wrong, should my subclass of UISplitViewController not be its own delegate? Then where do I define the delegate in code or otherwise?
Thank you for your time.
Edit: More info-
I tried putting self.delegate = self; in viewDidLoad, but that didn't seem to help.
The particular method I am trying to override is
splitViewControllerPreferredInterfaceOrientationForPresentation:
and I've put an NSLog in the code to notify me if it gets called, which it isn't
As far as I know, NSSplitViewControllers cannot have delegates, and their splitViews can't have their delegates reassigned since the controller acts as the delegate.
If you need access to the delegate methods, simply subclass the controller, then change the class name in Interface Builder.
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
In my current project I have a few viewcontrollers that use a UItextfield. As of now I have all the viewcontrollers set as delegates for the textField. However, I have all the same code for each of the viewcontrollers (at least for the UItextfield delegate methods). Is there a way I can just create a static class and set that as the delegate for all the UItextfields?
You can achieve using two ways.
Once create a singleton class that can handle your delegates and assign the singleton instance as your delegate.
You can inherit the UITextField say myTextField and implement all the delegate methods which you want to override. For all the instances where you want to use TextField and have the custom behavior use myTextField.
Is there a way to get a notification for the different UIScrollView delegate methods inside the subclassed scrollview without setting "self.delegate = self"? I need to get notification for certain events inside my subclass, but I still need certain other events to be sent to the delegate. So unfortunately I can't assign two objects as delegate. Are there ways to do this otherwise?
Your subclass has a weak property - oldDelegate - and when the delegate is set (you detect this in a subclassed setDelegate method), you actually set oldDelegate instead, then make yourself the delegate.
Implement all the delegate methods and send each to "oldDelegate".
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.