Custom Delegate for a custom View - ios

I m using Xamarin for iOS and I have a custom View which inherit from UIView.
I would like to add a custom delegate to that view.
So far I found that:
Delegate (not useful) example
I want my delegate to be on his own and won't inherit from any other known delegate.

There is no delegate property on UIView (see Apple docs). It does exists in some subclasses, like UITextView (and other types).
What you can do (beside using the base classes provided) is:
(with the unified API) create your own classes that implements the IUITextViewDelegate interface and assign it to the Delegate property;
Create any class that conforms to the delegate (i.e. minimally all required members), add the required [Export], and assign it to the WeakDelegate property.

Related

How do you implement the UIAccessibility protocol?

The "Accessibility Programming Guide For iOS" states:
Another way is to implement the isAccessibilityElement method of the UIAccessibility protocol in the implementation of your custom subclass. The following code snippet shows how to do this:
But when I am doing so, as in the following code:
class UITextLayerLabel : UIView, UIAccessibility {
XCode returns me this error:
[...] Use of undeclared type 'UIAccessibility'
What I am missing? A specific import? Is the protocol still available with iOS 10/XCode8?
As pointed out here, UIAccessibility is an informal protocol. For the difference between an informal protocol and a formal one, see here.
Basically, there is no type called UIAccessibility. Actually, UIView is already conforming to UIAccessibility. To make your custom view accessible, just override the properties and methods starting with accessibility.
So when do you need to implement UIAccessibility? As stated here,
You can use UIAccessibilityElement to provide information about an
icon or text image is not automatically accessible because it does not
inherit from UIView (or UIControl ). A view that contains such
nonview items creates an instance of UIAccessibilityElement to
represent each item that needs to be accessible.

How to add custom class as datasource and delegate to a NSTableView

I am writing an application for OSX in Swift. I use a NSTableView.
I created a custom class which implements NSTableViewDataSource and NSTableViewDelegate. Now I would like to mark this custom class as delegate and datasource of my TableView but, I can’t find how to do that.
The only way I found out to do that is : Create un Ojbect in the interface builder. Add it in my view put my custom class as class of the object. And finally link the object as datasource and delegate of the TableView.
This way doesn’t seem to be clean. Does someone have an idea to solve my problem ?
let myObject = MyClass()
tableView.delegate = myObject
That should do the trick. A bit cleaner than using Interface Builder.
Create an instance of your custom class that implements those protocols where you have your NSTableView. Then set the delegate and datasource properties to the instance that you created

How to show subclass of UIControl in View Controller

I would like to be able to interact with the UIControl I have made, and therefore want it in my ViewController.
What I tried
I subclassed UIControl (1).
Then I added a UIView to my View Controller and assigned it the new class (2).
But in Interface Builder I am not able to set my outlets to the buttons contained in the new class (1)?!
1:
2:
UIControl's documentation confirms that it is a subclass of UIView, and I should therefore be able to connect the outlets, right?
What am I missing here? :/
Off-course you can't add IBOutlet because buttons what you added to WeekdayControl are in UIViewController, you can't add Outlet to WeekdayControl, buttons only subviews of WeekdayControl, UIViewController is boss here, and you can add outlet only to UIViewController. (Sorry for my English)
Better create you buttons programatically in WeekdayControl.
Must read first:-
You cannot use the UIControl class directly to instantiate controls.
It instead defines the common interface and behavioral structure for
all its subclasses.
The main role of UIControl is to define an interface and base
implementation for preparing action messages and initially dispatching
them to their targets when certain events occur
So, you are doing wrong, if you really need to make a custom view or custom control then you can directly do it by creating a custom UIView and connecting the outlets directly with the view.
I think you missing the objective of subclassing a UIControl, it doesn't give rights to create outlets as it's a subclass of UIView,just read this lines what it is stated in the docs:-
Subclassing Notes
You may want to extend a UIControl subclass for either of two reasons:
To observe or modify the dispatch of action messages to targets for
particular events
To do this, override sendAction:to:forEvent:, evaluate the passed-in
selector, target object, or UIControlEvents bit mask, and proceed as
required.
To provide custom tracking behavior (for example, to change the
highlight appearance)
To do this, override one or all of the following methods:
beginTrackingWithTouch:withEvent:,
continueTrackingWithTouch:withEvent:, endTrackingWithTouch:withEvent:.

When subclassing in iOS, is itsafe to change the type of a property to a subclass of that property?

If I have a UIViewController subclass (MNIViewController) with a property called match - itself a subclass of NSObject (MNISportMatch).
Now if I make a subclass of the MNIViewController (let's say, MNIFootballViewController) I'd also make a subclass of the MNISportMatch class called MNIFootballSportMatch. In MNIFootballViewController is it safe to redefine the match property as being a MNIFootballSportMatch object instead of a MNISportMatch one?
All the attributes and methods of a superclass are also in the subclass. So if MNIFootballSportMatch is simply a sublcass of MNISportMatch I think it's perfectly safe.

Can a subclass of B (which inherits from class A) inherit from a subclass of A instead of A itself?

I'm working on an accessibility project for an iOS application. Because accessibility does not act quite as advertised, I have to override accessibilityFrame, accessibilityActivationPoint and pointInside:withEvent in a subclass in order to expand the region recognized by VoiceOver (for both drawing and touch recognition) beyond the "natural" bounds of the control view. So, in order to change the VoiceOver bounds of a UIButton I have to subclass that class and then add these three methods. In order to do this for a UILabel I have to add another subclass with the code, and so on.
I can refactor the code in these methods to a central location, but I was wondering if this can be done more elegantly with inheritance. I'd like to put this code into a subclass of UIView (maybe called UIViewAccessible) and then create a subclass of UIButton called UIButtonAccessible which inherits from UIButton which would in turn inherit from UIViewAccessible instead of UIView. Is this possible, or can something like this be done with a category?
Edit: According to the docs, you can't really achieve this with a category:
If the name of a method declared in a category is the same as a method in the original class, or a method in another category on the same class (or even a superclass), the behavior is undefined as to which method implementation is used at runtime.
Is there some other way to do this?
To answer your question, no, it can't, since your UIViewAccessible is a second degree sibling to UIButton in the inheritance chain (both inherit from UIView at some point). But I guess you already knew that. As for a solution, you could wrap around your UIView accessible classes a decorator and use protocols for strong typing. That way you'll keep the code in one place. I've described this technique here in more detail (although for a different purpose, it's the same situation).
For the views that would support accessibility you'll have to do this:
#property (nonatomic, strong) UIView<MyAccesibilityProtocol>* view;
//self.view can come from the nib or previously created in code
self.view = [[AccesibilityDecorator alloc] initWithDecoratedObject:self.view];
//you can then use self.view like any other UIView,
//and because it also implements an
//accessibility protocol, you can use the methods
//implemented in the wrapper as well.
//more than that, you can control which methods to override
//in the AccesibilityDecorator class
[self.view addSubview:otherView];//could be overridden or not
[self.view myAccesibilityMethod];//custom method declared in the protocol

Resources