I have a class that is derived of UITableViewController and handles everything related with a specific type of tables. Let's call it Class T
In may main application class, Class A, I have methods to populate other areas of the screen as, for instance, a map.
While I'm populating my table within Class T, I would like to call the Class A method that plots the x,y points on the map.
Is this possible? What should be the correct way to do this?
When I though about this approach, I was expecting that invoking [super ...] inside Class T would call the Class A methods, as this is the owner class of the instance, but ofcourse it call the parent class, in my case the UITableViewController.
Thank you,
Pedro
If A is your main application class, you should be able to access the application instance with [UIApplication sharedApplication], cast it to the class A type, and call the APIs you need to call.
Why not define a ClassAProtocol and then add a property "classADelegate" in Class T?
ClassAProtocol will define a method like:
-(void)plotXYOnMapFromData:(id)someObjectContainingDataToPlot;
So in the Class T interface you will add:
#property (assign) id classADelegate;
and then when you instantiate, let's say from instanceA (instance of Class A), instanceT (instance of Class T) you will do:
instanceT.classADelegate = instanceA;
Finally inside Class T you can call the plotting method in this way:
[classADelegate plotXYOnMapFromData:myDataToPlot];
The advantage of the delegate pattern in this case is that Class T just need to know only one small piece of ClassA, which is the protocol, and ClassA is able to communicate with T thanks to its implementation of the protocol.
Related
I am trying to understand Class type in Objective-C. May I ask what is the different between Class type and NSObject type? It sounds like Class type does something similar like NSObject correct?
I believe this has been asked several times... but I couldn't find any old instance, so anyway.
Unlike C++-like languages, classes are actual regular object instances in OBJC.
When you call [NSObject class], it returns a live object which contains class methods and extra informations. These are synthesized by compiler, and this actually provides all the class related features.
Class is the type for for these class objects.
One more thing. Because the Class objects are objects, they also have class object, and these are called meta-class. See here for more details:
http://www.cocoawithlove.com/2010/01/what-is-meta-class-in-objective-c.html
Also, you may be confused because most of types in Cocoa are using NSObject as a root class. But it is just a convention, and actually a class doesn't have to subclass NSObject.
No NSObject is not the same as Class.
In Objective C, classes are objects. A class like NSObject in an instance the Class type. It is correct to say NSObject is of type Class, just like you would say an instance of NSObject is of type NSObject.
Class is a an object also, but it's type is the meta-class which really isn't for beginners.
A class is a template, e.g. a human
An object is an instance of a class, e.g. bob the human
an NSObject is the root class of most Objective-C classes (e.g NSTextField, NSButton, etc)
So, NSButton inherits the properties from NSObject, and is also a class (a child class). If you create a button, that button is now an instance (object), of type NSButton (class), which inherits from: NSControl : NSView : NSResponder : NSObject in that order, descending.
More info:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
Totally being a newbie by to objective C this perplexing question occurred to me .
So here it is,
Lets say say we have a viewcontroller that implements UITableViewDelegate.If we extends this viewcontroller does the child class inherits the UITableViewDelegate callback methods. So we can override the certain callbacks like cellForRowAtIndexPath etc. from the child class.
Have a good day
Yes, absolutely. The child class will also conform to the protocol. The implementation of these methods is taken in the superclass, or in the child class if it is redefined in it.
I'm using a Objective-C framework for game development called Cocos2d-iphone.
This is how I create a button-graphic in the game:
CCMenuItemImage *battle;
battle = [CCMenuItemImage itemFromNormalImage:#"BattleFightOption1.png" selectedImage:#"BattleFightOption2.png"
target:self selector:#selector(battleFightOption)];
Basically, when the user clicks the button, method battleFightOption runs.
But I wonder, I never did define battleFightOption in the interface.. so, my question is: when is it necessary to define a method in the interface, and when is it not?
In short, every method that is meant to be used from outside the class must be declared in the interface; methods that are internal to the class implementation are omitted. The latter are typically declared in a class extension.
When you use a selector like #selector(methodName:), methodName: is called dynamically at runtime. The compiler doesn't have to know where it is, and doesn't check that the method exists when you compile.
However, it is still a good idea to declare it privately, which is generally done by putting an unnamed category at the top of the .m file (generally referred to as a class extension):
#import "Class.h"
#interface Class ()
- (void)privateMethod;
#end
#implementation Class
...
Anything that you intend to be public, called outside of the class, should be defined in the interface. If you are going to only use #selector(battleFightOption) you really do not need to define the method anywhere but I would recommend that you add a definition in the class extension just as you would any other private method.
I know of a couple of rules regarding Objective-C categories:
Category methods should not override existing methods (class or instance)
Two different categories implementing the same method for the same class will result in undefined behavior
I would like to know what happens when I override one of my own category methods in the same category. For example:
#interface NSView (MyExtensions)
- (void)foo; // NSView category implementation
#end
#interface MyClass : NSView
{ }
#end
#interface MyClass (MyExtensions)
- (void)foo; // MyClass category implementation
#end
With these interfaces defined, which method will be executed when I run the following code?
MyClass * instance = [[MyClass alloc] initWith...];
[instance foo];
[instance release];
Note: With my compiler, the MyClass implementation takes precedence, but I'm not sure if that is guaranteed to occur, or simply one specific flavor of undefined behavior.
To extend on drawnonward answer:
It's matter of hierarchy. Categories are really just a means of organizing source files. When compiled, all the methods of a class, including the ones defined in any category, end up in the same file.
Anything you could do in a regular class interface you can do in a category and anything you shouldn't do in a regular class interface you shouldn't do in a category.
So:
Category methods should not override
existing methods (class or instance)
You can use methods defined in the regular class interface to override inherited methods so you can override inherited methods in a category.
However, you would never try to have to two identical method definitions in the same ordinary interface so you should never have a method in a category that has the same name as a method in either the ordinary interface or another category on the same class. Since all the method definitions end up in the same compiled file, they would obviously collide.
Two different categories implementing
the same method results in undefined
behavior
That should be rewritten to say "Two different categories implementing the same method for the same class results in undefined behavior." Again, because all the methods for any one class end up in the same file, having two methods in the same class would obviously cause weirdness.
You can use categories to provide methods that override superclass methods because a class and its superclass are two distinct classes.
If your ever confused about whether a category will cause problem just ask yourself this: "Would the methods in the category work if I copied and pasted them all into the class' .h/.m files?" If the answer is "yes" then you're in the clear. If "no", then you've got problems.
Each method of each class has an implementation. A category adds or replaces a method for a specific class. That means the behavior you are seeing, where MyClass has one foo and NSView has another foo, is well defined. Any instance of MyClass will have a different foo than any instance of NSView that is not a MyClass, just as if foo had been defined in the main implementation and not a category. You should even be able to call [super foo] from MyClass to access the foo defined for NSView.
I have a questions about the iPhone application. I am the green of the iPhone application. When I read the document(PDF) download from the apple developer website (online version). I found that the document always mentions different methods of the library.
There are
1) Class method
2) Instance method
3) Delegate method
I understand the use and meaning of the instance method, which is called by a instance.
let's say the delegate methods is the connection:didReceiveAuthenticationChallenge and the class method sendSynchronousRequest:retruningResponse:error:.
However, I don't understand about the different between the class method and the delegate method. Is the class method for the whole class? or whole project? What it means of the delegate? and where should I put the code after I modify the content of the delegate? How can I call the method?
Can anyone help me. Thank you very much.
It is another question about the delegate method. And I don't how to solve the problems. Please help me. Thank you.
HTTP status code = 0 (iPhone) (objective c)
Suppose you have a class Foo and an instance of that, Foo* foo.
Then, the class method is a method which is sent to the class:
[Foo classMethod];
while the instance method is a method sent to the instance:
[foo instanceMethod];
The delegate method is a method which the instance of the class calls. So, you typically implement another class Delegate with an instance Delegate* delegate, and do
[foo setDelegate:delegate];
Then, the object foo calls the delegate method of delegate at appropriate times:
[delegate delegateMethod];
This is a way to receive an event from the system API.
Apple provides extensive documentation on the fundamentals for Objective-C and Cocoa - if in doubt, this should be your first stop.
The Objective-C Programming Language - Class Objects:
[...] a class definition can include methods intended specifically for the class object—class methods as opposed to instance methods. A class object inherits class methods from the classes above it in the hierarchy, just as instances inherit instance methods.
Cocoa Fundamentals Guide - Delegates and Data Sources:
A delegate is an object that acts on behalf of, or in coordination with, another object when that object encounters an event in a program.
The delegating object is often a responder object—that is, an object inheriting from NSResponder in Application Kit or UIResponder in UIKit — that is responding to a user event. The delegate is an object that is delegated control of the user interface for that event, or is at least asked to interpret the event in an application-specific manner.
And some related background in The Objective-C Programming Language - Protocols:
Class and category interfaces declare methods that are associated with a particular class — mainly methods that the class implements. Informal and formal protocols, on the other hand, declare methods that are independent of any specific class, but which any class, and perhaps many classes, might implement.
A delegate method is a method that is defined in a classes delegate protocol. They are added to your class but your class must have the objects delegate protocol. They are usually used by the object but is something that you must define for the object. NSTableView and UITableView use delegate methods to populate their data. A class method is just one that you define in your interface.