Adding Two different Delegates in iPhone - iphone

I am new to iphone app development. I want to add two different delegates.
I know how to add multiple delegates of same class like
UIViewController .
I want like this
#interface HelloWorld : CCLayer, UIViewController
in which one class is CCLayer of cocos2d and second one simple UIViewController.
Thanks in advance for help.

I'm not familiar with CCLayer but you can't do multiple inheritance in Obj-C (so you can't have two superclasses). What you can do is create two classes, one that subclasses UIViewController and the other subclasses CCLayer and then create a protocol/delegate pattern to allow communication between the two.
http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

Related

iphone development: is it possible to extend more than one viewControllers?

in my app I want to use googleAnalytics. To use it I have to extend GAITrackedViewController but the problem is I already extend GLKViewController because my view has an openGL application. So is it possible to extend the properties of both view controllers?
For a similar case, I've simply created a subclass of UIViewController (GLKViewController in your case). That subclass handles the tracking of the view. All "specific" ViewControllers extend that custom UIViewController, instead of the default one.
Then you could, for instance, track the view manually:
id<GAITracker> tracker = [[GAI sharedInstance] defaultTracker];
[tracker sendView: NSStringFromClass(self.class)];
No, there isn't multiple inheritance in Objective-C. You have write a subclass of GAITrackedViewController and a subclass of GLKViewController separately, and write a controller class that has an instance of these two classes, coordinating them.
It's not possible, objective-C doesn't support multiple inheritance. You should take a look to this question: Objective-C multiple inheritance
it is very bad to extend 2 classes even on languages that allow it,
because you can get 2 ways to your "super" and this is a way to many bugs

Declaring delegates on .m

I am relatively new to Objective-C.
I have come to a code on the web that has something like this on rootViewController.m (this is a navigationController based app).
#interface RootViewController (CManagerDelegate) <CManagerDelegate>
#end
#interface RootViewController (PViewDelegate) <PViewDelegate>
#end
two questions:
what are these lines doing in the beginning of rootViewController.m
what are these lines doing in code? Please explain the stuff in parenthesis and between <> in this particular case.
thanks.
In one sentence: The code you posted makes the RootViewController class privately conform to some delegate protocols.
Delegate protocols are used to let a class declare the fact that it understands the messages from another class's objects. For example, a view controller can declare that it understands a gesture recognizer's delegate messages.
The fact that the class internally uses the gesture recognizer is often an implementation detail not relevant to other clients of the class. It is best not to publish this fact in the public interface but put it into the implementation (.m file).
Categories (and class extensions) let you do exactly this: Make a class conform to a protocol without changing the main #interface.
A nice and elegant solution!
Read up on Categories:
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/objectivec/chapters/occategories.html
And Protocols:
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/chapters/ocProtocols.html
In fact, read all of Apple's Objective-c documentation before going any further:
http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectivec/Introduction/introObjectiveC.html
Good luck.

UIView Subclass not working

I have an XCode project I am working on that has multiple views controlled by a tabBar that is throwing multiple instances of the error:
"cannot find interface declaration for '~my sub view name~', superclass of '~my sub view name~'
These sub views were created after the original project as new files. Each has a line of code like this:
#interface meViewAndEdit : meViewAndEdit
I see no #import statements either. The project fails to build and I am not sure what i should be setting these to. Should I be referencing my App delegate as the superclass? what is missing here?
UPDATE: (updated)
I changed the interface statements for each of my header files for the views i created as follows
#interface friends : UIView
BUT, it seems that i have a new issue that i'll have to research:
"UIView" may not response to "initWithNibName.bundle"
This is now present in each of the .m files for the views I created.
still learning, thanks in advance for your input.
When declaring a class in Objective-C, you need to specify the class name, as well as the superclass. In your code #interface meViewAndEdit : meViewAndEdit, you are essentially declaring a class that is a subclass of itself. Since you are trying to create a subclass of UIView, the class declaration should look as follows: #interface meViewAndEdit : UIView.
Also, in a comment in another answer, you asked whether or not you need to #import the app delegate in every class. The answer to this is usually no, unless you specifically need to access something involving your specific app delegate class.
I will also make note that it is conventional to give classes a capitalized name. For example, it should be MeViewAndEdit, rather than meViewAndEdit. You only should keep the first letter lowercase if it is the name of a variable or function.
EDIT: The reason that you are encountering the errors in your update is that you are trying to subclass UIViewController, not UIView. On top of this, instead of subclassing either one, you are subclassing the app delegate. Change your line of code #interface subviewname : my_app_delegate to #interface subviewname : UIViewController. You are trying to create a subclass of UIViewController, not my_app_delegate.
On another conventional note, it is never good to put underscores in a class name. Always name classes in camel case like MyAppDelegate, not my_app_delegate.
You need to #include the .h header files in all classes in which you are using them.

what is the difference between NSObject and UIViewController

i am new to iphone development and i am going through tutorial and some sample.
i want to know what is the difference between NSObject and UIViewController class and how we will come to know which class we should use.
some are written in NSObject and some are in UIViewController.
From Wikipedia, a basic overview of object-oriented programming:
Object-oriented programming (OOP) is a
programming paradigm using "objects" –
data structures consisting of data
fields and methods together with their
interactions – to design applications
and computer programs. [...] An object-oriented program will usually contain different types of objects, each type corresponding to a particular kind of complex data to be managed or perhaps to a real-world object or concept such as a bank account, a hockey player, or a bulldozer.
In Objective-C, all Objects are based upon NSObject. Just take this at face value for now. If you want to use an Object, it's going to be based on NSObject. So, unless you're using an int or a float, you're likely using something that's based on NSObject.
NSObject in-and-of-itself, doesn't really supply any functionality. It's your 'starting place' or 'blank slate' for an Object.
You might build an Object definition which is used to represent an Animal, like this:
#interface Animal : NSObject { }
#property (assign) int age;
- (Animal*)mateWith:(Animal*)lover;
#end
In this example we've described a basic Animal. This class basically does two things; knows the age of the Animal, and can mate with another animal to produce an Animal offspring.
You'll notice in that example that we based our Object definition on NSObject.
Now, say we want to create a definition for a Human; well, a Human is, and always will be, a subset of all Animals. So, we can re-use all of the logic in the Animal class definition to create a Human definition - and we might do so like this:
#interface Human : Animal { }
- (void)lie;
#end
In this example, we've created a new definition for a type of Object called "Human". We only defined one thing: a method which gives our class the ability to lie - except we'll also get the ability to mate because we're based on "Animal", and "Animal" already describes how to mate.
Getting to your question:
UIViewController contains a BUNCH of logic for doing some very complex tasks. Most of that logic is part of the Cocoa Touch framework.
If you're making an "Animal" class, you don't need to know how to respond to user input from the screen, you don't need to know how to manage a UIView, you don't need to keep track of parentViewControllers, etc. So, basing our Animal class on UIViewController would be silly. This is when NOT to use UIViewController.
Now, if you've making a user interface screen on the iPhone, and you want to perform some routine when the user clicks on a button - then you DO need all of the UIViewController stuff, so you'd subclass that.
I can understand why, if you're not coming from an Object Oriented Programming background, you might be confused about this. It seems like most of the things you'd need to create ARE UIViewController subclasses. However, as you explore the world of OOP, you'll discover that not only are Objects something someone else wrote that you can use - but they are things you'll want to create from the ground up to accomplish things you used to do procedurally.
Best of luck on your exciting journey.
I'd highly recommend you take a trip to your local Barnes and Noble or head over to Amazon.com and pick up some books on the topic - if you have a friend who already knows OOP a good mentor is much faster than learning yourself.
Don't forget, on the iPhone, you'll have to deal with memory management as well. This is a sticking point for a lot of people - and causes a lot of headaches if you don't follow the rules. Learn them early and you'll be served well.
Hope that helped,
Cheers.
Sources:
http://en.wikipedia.org/wiki/Object-oriented_programming
UIViewController is a subclass of UIResponder which is itself a subclass of NSObject which is a root class (i.e. not a subclass of anything).
This means that any method for NSObject may be called on UIViewController, but not conversely. If you look at the UIViewController Class Reference, it has all of the properties and methods available to you if you use this class. In addition, you automatically get all of the methods for an NSObject (listed in the NSObject Class Reference).
I use a UIViewController for every view that I maintain. I almost never use an object directly as an NSObject, though I often subclass NSObject and I never subclass UIViewController. But this is just me.
I recommend you take a look at Apple's View Controller Programming Guide to see what benefits using a UIViewController offers.
NSObject means it inherits the ObjectProperties only.It doesn't have view.But UIViewcontroller is having the view itself.It can control the views also.
When you don't want a view then you can use the NSObject.If you need viewcontroller or view then you can use the UIViewController.
NSObject is the super class in obj_c and UIViewcontroller is sub class to NSObject.UIViewcontroller inherits properties from UIResponder and this class inherits from NSObject class...

best way to make base uiviewcontroller iphone

I an making an app which has many different uiviewcontrollers but they share the same functionality. I am going to make a basecontroller so the other classes extend this. However there are about 5-7 variables that I need to provide to the basecontroller. What is the proffered way to do this? Making a constructor that takes 7 parameters or is there better ways doing this?
Thanks in advance.
Subclassing will do the trick:
//different controllers that you want to implement
//it should subclass the base controller, as in this case, the SecondLevelViewContr
#interface CheckListController : SecondLevelViewController {
//and remember to implement your base controller
#interface SecondLevelViewController : UITableViewController {