Should we call a category method from self - ios

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.

Related

UIView instance in UIViewController

Assume I have two UIViewControllers and aPlayerView (UIView Class). And I need thePlayerView in both controllers.
Is there a way I could load thePlayerView without creating two different instances of the same class. So if I update something to thePlayerView in the first controller, the second controller displays the updated view.
Now, I alloc init PlayerView in every UIViewController I need the class in.
One way is to create a property ofAppDelegate and subview it to everyUIViewController I need, but I don't know if this is the best way to go.
You should follow MVC (Model-View-Controller) pattern when building your apps.
In MVC, your PlayerView just have to display a state.
The state is stored in a Model (i.e. an NSObject with properties, these properties being the state of your game).
Then, you can instantiate as many PlayerView as you want or need. Just be sure to give them access (in the init step for instance:
- (instancetype) initWithState:(GameState *)<yourUniqueModelObject>
to your Model object so that they can display according to your game's state.
Your Model object is unique to your application and is shared across all your PlayView when needed.
Then, if you need, you can add a Controller that will handle the logic of your app/game/interface, and make the link between your Model and your View.

Hiding a category from other classes

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?

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

xcode using view in subclass

I am learning to program the iphone and I wanted to do some drawing. I followed some example code and subclassed the viewcontroller and it worked fine. Now as I wanted to expand the program I came upon a design question that I could use a little help on.
I subclass myviewcontroller with mynewview. If I have any code in the myviewcontroller how do I call or reference it in mynewview and vice versa? I am not sure if I am asking this right but I am trying to understand the relationship between the class and subclass.
Objective-C objects benefit from inheritance. All classes are subclasses of NSObject, therefore you can call init on any object. If you created a custom class and gave it a method doSomethingAwesome, you are free to then implement doSomethingAwesome in any subclass of your custom class. However, declaring a method in a subclass does not add that method to the superclass. As an aside, I rarely find myself subclass sing my own custom classes. I believe that it is encouraged to maintain what is called a shallow object hierarchy. Usually I subclass the stock cocoa classes, customize to my needs and if I need custom methods in more than one subclass I will declare a category on the superclass rather than relying on inheritance to provide my custom behavior
The messaging system in Objective-C is dynamic. Every object includes a struct with information that the runtime use for introspection. Here the runtime will find a list of methods the object is able to respond. So, let's say you message an instance like this:
[mynewview someMethod];
The runtime will first check the object information to trying to find some method that will be able to respond the message. If nothing is found, then will query the super class, and so on. In fact, the runtime is much more complex, and will give any object more opportunities to respond (that's the dynamic part. For instance, mynewview might not have any method called someMethod and yet, might be able to satisfy the call, but that's something you might not want to worry right now).
From a child class you can call the superclass implementation of a given method with the keyboard super, so if mynewview is a subclass of myviewcontroller you can call myviewcontroller implementation from mynewview with:
[super someMethod];
If someMethod is both present in myviewcontroller and in mynewview, the runtime will automatically only call the child implementation, you have to call the parent implementation (if you have to) from the child implementation.

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