Hiding a category from other classes - ios

I've written a category for UIImagePickerController which I would only like to be accessed by one specific class. The category contains overrides for two private methods. The problem I'm having is that no matter how I try to define the category it seems that every class that instantiates a UIImagePickerController will override these methods. From my understanding this is the expected behaviour when creating a category, but is there any way around it?
I've tried defining the category in a separate .h/.m file as well as adding it directly to a separate class's implementation but neither seem to work. I've also tried creating an additional category which calls super for these methods but the original category still seems to take priority.
Any ideas or is this just not possible?

Related

Should we call a category method from self

It is basically a design question. I have a controller with large number of code lines in my application. I want to reduce the number of lines and for that I used a category.
Like If I have UIViewController subClass and I created a Category on that sub class. Now I want to call a category method from the UIViewController subClass.
I have question should I call [self categoryMethod] or not ?
Thanks for the reply!
Yes you can able to call category method with self object as its a object of same class UIVIewController so not any issue.
Here are some more detail why we need Category:
You use categories to define additional methods of an existing
class—even one whose source code is unavailable to you—without
subclassing. You typically use a category to add methods to an
existing class, such as one defined in the Cocoa frameworks. The added
methods are inherited by subclasses and are indistinguishable at
runtime from the original methods of the class. You can also use
categories of your own classes to:
Distribute the implementation of your own classes into separate
source files—for example, you could group the methods of a large
class into several categories and put each category in a different
file.
Declare private methods.
You add methods to a class by declaring them in an interface file
under a category name and defining them in an implementation file
under the same name. The category name indicates that the methods are
an extension to a class declared elsewhere, not a new class.
So based on this description you can distribute your UIViewController code in small small pieces (files) as category and call it from your class.
One more way to reduce your Single class code is divide your ViewController in small part like You have TableView , Header View and Footer View , Navigation View so you can create sub class of this all View and write related code into that respective class and the main ViewController Class only contains data passing and general methods.
Use ContainerView is best practice to distribute your UI and code in small-small part.
Hope this will helps you to understand what you required!
Happy Coding.

Adding Additional Properties to the UIView in ObjectiveC

I'm trying to add custom properties to the UIView so I can use them among all UIView objects and all the UIView's subclasses, like UIImageView, UISlider ... etc
I've tried to use Category to do so, but it turns out I can't use instance variables in the Categories' properties. So, I canceled this solution.
I, also, have tried to use Inheritance to do so, as if I made UIView subclass and added all the properties that I want to. But in this case I do get my additional properties for all of my custom class instances, but I don't get them for any of the other UIView subclasses, like UIImageView.
I'm trying to figure it out but I couldn't.
At the end, I figured a solution to my problem. I'm not sure if it was the best one but it does work for me.
I've used a runtime feature of the Objective-C 2.0 which called: Associated Objects.
This feature gives me the ability to add custom properties to any class using Categories.
I found those resources helpful:
• http://nshipster.com/associated-objects/
• http://www.youtube.com/watch?v=VxUjilPe5Do

Objective C UIViewController subclass?

Recently I've been looking into RESideMenu. What really piqued my interest was his 'UIViewController+RESideMenu.h/m'. It appears to me that this is a way to subclass a UIViewController. It includes IBActions and methods in it that are accessible from anything that inherits from UIViewController.
All of my attempts to replicate this have failed. Is there a special way to go about it?
Usually that sort of classname is used to denote a category. Instead of subclassing it adds additional methods and properties to a class. For more information see here: CustomizingExistingClasses

Inheritance In Objective - C?

Okay, so my question itself states what i am asking.
I have a 3 view controllers. All the three of them are inheriting from a class (MainViewController).
Now what i want to ask is, is it possible in Objective C that suppose I have another class which has some variables and functions which i do not want to include in the MainViewController class, and pass this to one of the ViewControllers.
So if i number my view controllers, and let the other class be SecondClass.
1st, 2nd and 3rd ViewControllers inherit functions and variables from MainViewController.
If i want 2nd ViewController to inherit SecondClass also, then am i allowed to perform this kind of operation in Objective - C??
Objective-C is a single-inheritance language. Each class can inherit from exactly one superclass.
You can work around that with forwardingTargetForSelector: if you're absolute desperate but the neater and usually smarter thing is to compose functionality by saying that the second view controller owns an instance of SecondClass in addition to being a subclass of MainViewController.
You can see an example of Apple doing that in UIWebView. It isn't a subclass of UIScrollView but it uses a scroll view and exposes that instance (as of iOS 5) for outside actors via a property, scrollView.
Objective-C does not allow multiple inheritance. Check out this post for a great solution that uses composition.
Objective-C multiple inheritance
In objective C there is no support for multiple inheritance. Instead you can use either protocols or categories.
In addition you can use dynamic typing as well. Check out following link.
http://support.apple.com/kb/TA45894

Object Properties From Custom Classes in iOS

This should be easy - but I am scratching my head over it. Here's the problem:
I have a custom class that I import into my MainViewController.h file. The class contains a bunch of properties and a few methods.
I instantiate an object of this class in the ViewDidLoad section, and can run any of the methods or alter the properties on this object at will... they work fine as long as I stay within the ViewDidLoad section.
I created a button with which I wish to activate one of my custom class methods on this object, but the IBAction section seems out of scope, as it doesn't see the object created in ViewDidLoad at all.
Can someone point me in the right direction to enable these two areas to see eachother?
Thanks in advance!

Resources