0

As we all know the inheritence hierarchy of UILabel is:

UIView : UIResponder : NSObject

Now I want to change the UIView in the hierarchy to my custom UIView like:

CustomView: UIResponder : NSObject

So that all UILabels in my app Will have my custom UIView in their inheritence hierarchy.

Anindya Sengupta
  • 2,539
  • 2
  • 21
  • 27
AUK4SO
  • 301
  • 2
  • 13
  • So you want it to be like `UILabel` -> `CustomView` -> `UIView` -> `UIResponder` -> `NSObject`??? You really want to get that `CustomView` between `UILabel` and `UIView`. – Popeye Mar 25 '14 at 13:58
  • @Popeye I want like this - UILabel: CustomView: UIResponder: NSObject - UIView should be replaced with CustomView – AUK4SO Mar 25 '14 at 13:59
  • I'm sorry to say I don't think that is possible. I don't think you can amend any of `UIKit`s classes by changing the super classes. I will stand corrected if someone else has anything to say but unfortunately I don't think this can be done. – Popeye Mar 25 '14 at 14:01
  • What are you trying to achieve? Perhaps there is another way to reach your goal. – Robotic Cat Mar 25 '14 at 14:05
  • @RoboticCat I am implementing Theme color change for my app. As of now I have subclassed UIView(CustomView) and added a NSNotification Observer Where I am changing the backgroundColor property. All UIViews in my app are instances of CustomView. The same thing I want for UILabels, UITextViews,......As UILabel,UItextViews inherit from UIView I want to replace UIView in the inheritence hiererchy with CustomView. Like UILabel:CustomView:UIResponder:NSObject – AUK4SO Mar 25 '14 at 14:08

1 Answers1

2

That is not possible.

However, you can look for extending your class using a category. A category allows you to add methods to a class, but does not allow to change them. Here is a tutorial: http://www.tutorialspoint.com/objective_c/objective_c_categories.htm

Depending on what you are trying to achieve, subclassing UILable may still be an option. What exactly are you up to?

Edit: Well, theoretically it is possible chaning an object's superclass during runtime. However, I am not sure whether I really should recommend that to somebody who just asked your question. It is rather advanced, if advisable at all, and should be done by programmers who know exactly what they are doing. However, see this questions and answers: My isa-swizzling breaks KVO Dynamically change an object's superclass

Community
  • 1
  • 1
Hermann Klecker
  • 14,039
  • 5
  • 48
  • 71
  • I am implementing Theme color change for my app. As of now I have subclassed UIView(CustomView) and added a NSNotification Observer Where I am changing the backgroundColor property. All UIViews in my app are instances of CustomView. The same thing I want for UILabels, UITextViews,......As UILabel,UItextViews inherit from UIView I want to replace UIView in the inheritence hiererchy with CustomView. Like UILabel:CustomView:UIResponder:NSObject – AUK4SO Mar 25 '14 at 14:07
  • Well, for such a thing I personally would go for categories. That is more or less what they are made for. And please ignore my hint about changing the superclass on runtime. That part of the answer was just added to make it academically correct, not to make it more practical :-) – Hermann Klecker Mar 25 '14 at 14:10
  • BTW, I did implement a similar functionality in the past. For doing so I implemented a superclass of all my view controllers and table view controllers. On viewWillAppear they went through the total subview hierarchy. It turned out to be much work than I ever thought it would. The solution was highly volatile to each iOS update and most of it became redundant when the `appearances` where introduced. Plus, the solution was not really OOP and some sort of 'hack' to a certain extend because it turned out that I had to consider undocumented UIView-Subclasses. – Hermann Klecker Mar 25 '14 at 14:15
  • That was my first choice too. I am trying to make it a component so that I can use in any of my other projects. Just checking for the possibilities. – AUK4SO Mar 25 '14 at 14:21