my subclass from UIPopoverBackgroundView can't access UIPopoverBackgroundView's instance variable - ios

I made a class which is subclass of UIPopoverBackgroundView:
#interface TestPopView : UIPopoverBackgroundView
#end
Where i tried access the property arrowOffset of UIPopoverBackgroundView class.
but when i implement a method in my subclass ,and i want to access arrowOffset directly (using the form _arrowOffset ,not using getter&setter) , the compiler complains "use of undeclared identifier _arrowOffset
what's the reason ,i can't acess the property directy?

The instance variable _arrowOffset is private iVar to superclass and thus us not directly accessible to subclass. You can use getter/setter to use it in subclass, which works.

Related

Modify (or add) property attributes of parent class

I'm working on building a UICollectionView subclass to cover up more convenient layouts in a simple way. It needs to have another delegate property to handle messages from custom UICollectionViewLayout class, which the UICollectionView class already has, so I wish the delegate property of UICollectionView handles both UICollectionViewDelegate and another custom delegate protocol.
More specifically, I'd like to build something like the followings.
#interface CPGridCollectionView : UICollectionView <CPGridLayoutDelegate>
// add protocol declaration to existing delegate property
#property (nonatomic, weak, nullable) id<UICollectionViewDelegate, CPGridLayoutDelegate> delegate;
// more additional features
#end
But warning came up as:
Auto property synthesis will not synthesize property 'delegate'; it will be implemented by its superclass, use #dynamic to acknowledge intention
It seems that I need to add attributes to existing property of UICollectionView, without breaking the functionality of UICollectionViewDelegate. Is this possible in Obj-C, and how can this warning be avoided?

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 Subclass use a delegate method of superclass Ios

Is it possible to use a delegate of superclass on subclass
Example: Class A is superclass of class B and superclass A uses the text view method example textviewDidChange. can I somehow call [super textViewDidChange] of superclass A on Subclass B even it isn't on header file of class A
if i rediclare the method on subclass B
When you specify UITextViewDelegate, you're telling the compiler that your class meets the specifications for a UITextViewDelegate. If class A has all the requirements for a UITextViewDelegate, and class B is a subclass, then it too will have all the requirements. Nothing will stop you from assigning any instance as a delegate, so you still have to be careful with that.
Yes,you can. Since class A confirms <UITextViewDelegate> protocol there is no need to redeclare them in header file. Just make sure it implements needed methods.

Inheritance: Restricting rather than extending? [duplicate]

This question already has answers here:
Is it possible to make the -init method private in Objective-C?
(9 answers)
how to block a superclass method to be called to a subclass
(5 answers)
Closed 9 years ago.
Suppose you have a UIView subclass. You define an init method "myInitWithFrame: ... andWhatNot:...". You know you won't be using the init method inherited from UIView ever and your custom init method does some vital custom initialising so that you want to force client classes to never use the inherited initWithFrame method.
Is it possible to hide the standard initWithFrame method that was inherited from UIView?
Actually, you can get compile-time warnings about calling a method on a subclass. Use the __attribute((deprecated)) attribute. If you want people to use -initWithPizza: instead of -initWithFrame:, do this:
#interface MyView : UIView
- (id)initWithPizza:(MyPizza *)pizza;
#end
#interface MyView (Deprecations)
- (id)initWithFrame:(CGRect)frame __attribute((deprecated("Use initWithPizza: instead")));
#end
Putting the -initWithFrame: declaration in a separate category is necessary to avoid Xcode complaining that you declared the method in the header but didn't implement it. Since you're just inheriting it from the superclass, that's fine; you don't have to implement it at all. But if you want to implement it to throw an exception, or call through to -initWithPizza: with a default argument, that's fine.
Of course, this won't stop UIKit from calling -initWithFrame: if it was already going to do so. But if you can guarantee that won't happen, then you're fine.
Actually, you CAN restrict with a subclass. You can override whichever methods you want to block in your subclass's .h file. You can make initWithFrame unavailable by placing the following in your .h file.
- (id) initWithFrame:(CGRect) frame __attribute__((unavailable("message")));
This will make the initWithFrame: method unavailable to anyone using your subclass.
To keep other code form calling this method, you can further restrict by putting this in your .m file.
- (id) initWithFrame:(CGRect) frame
{
return nil;
}
No. You can't prevent the users of your subclass from calling the methods of a superclass. You could override them and throw an exception inside, but that would just produce a broken subclass.
Remember that inheritance works as an "is a" extension, that is, instances of your subclasses should behave normally in any context that doesn't know about this particular subclass but knows about its superclass. It's only in places that have explicit knowledge about your subclass that you can benefit from adding extra initialization and other methods.
For example, UIKit has no knowledge of your subclass. So if you want to make your UIView subclass available from a NIB, you need to use the initialization methods that will be called by the NIB loading system, namely initWithCoder:. You can simply call your own initialization methods inside initWithCoder:. But if there are any additional parameters you would like to pass to the init method, you'll have to provide a way to configure them after initialization.

How to make My UIControl subclass as superclass of programmatically created UIElements?

In my application I want to associate NSMutableDictionary to all UIElements (UIButton, UILable, etc), I can easily achieve this by subclassing each element, but I just want to know if I can make my own UIControl subclass -with a property of type NSMutableDictionary-, as superclass of all programmatically created UIElement s in anyway, so that I can reduce the number of subclasses.
Here like this
#interface UIControl : MyControls
{
}
#property(nonatomic,retain) NSMutableDictionary *details;
#end
and make MyControls as superclass of all programmatically created UIElements
You can do this, not by subclassing, but by adding your own properties and methods to UIControl itself (the superclass of UIButton, UILabel, etc.). These will then be inherited by any standard buttons, labels, etc. that you instantiate. Objective-C lets you add your own methods very easily using Categories. However, you can't add instance variables via categories. To do that, you need to use Associative References which are documented in the Objective-C Runtime Reference.
There's a good tutorial on how to do this here.
By the way, I don't necessarily disagree with inturbidus. But if you're sure you want to go this route, that's how you'd do it.

Resources