Does a subclass inherit delegate callbacks? - objective-c

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.

Related

Is it OK to override UIViewController methods on a category on UITableViewController

I know you should not override methods in a category that are defined in the class the category is for. But what about overriding inherited methods. Is that OK?
Specifically, UITableViewController inherits methods like viewWillAppear:, viewWillDisappear, viewDidLoad: and so on, from UIViewController.
So, let's say in a category on UITableViewController, we override those methods inherited from UIViewController.
Is it OK?
As far as I can tell, this will only break if Apple in some future version of UIKit decides to override these methods in UITableViewController.
Are there other reasons not to do this?
EDIT:
So the part about overriding methods in categories in the documentation passed under my radar, so thanks for the answer.
I'll have to solve this with subclassing and possibly extensions.
Overriding methods in a category is discouraged. See Overriding methods using categories in Objective-C
In my opinion it doesn't matter if this means overriding an inherited or class-defined method. Why should it make a difference? Why not subclass?

why must i inherit NSobject instead of NSapplication to implement delegate method on GNUSTEP?

I've seen several Obj-C tutorials. The delegate classes all inherit from NSObject. For example, the applicationDidFinishLaunching delegate method, in some tutorials, it inherited from NSObject but NSApplication to implement it. The reason I don't think it should inherited from NSObject is that I didn't find any delegate protocol declaration in it, but I found that delegate protocol declaration in NSApplication. My Objective-C toy environment is GnuSep.
Here is some code:
#interface browserController : NSObject //here. inheriting from NSObject,but NSObject don'have any protocols declaration about applicationDidFinishLaunching.
{
NSBrowser *browser;
}
#end
#implementation browserController
- (void)menuAction:menuItem
{
..............................
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSWindow *win;
ActiveBrowserDelegate * abd;
WindowDelegate *wd;
NSRect wf = {{100, 100}, {600, 500}};
NSRect bf = {{10, 10}, {580, 350}};
.............................
}
It is called informal protocol (though GNUstep declared it anyway as GSAppDelegateProtocol for documentation purpose) NSApplication will simply check it at runtime if your delegate object will respond to the message, (using -respondsToSelector:) A delegate can be a view, a string, a proxy, anything as long as you make it responds to the selector. You don't need to make your delegate implement every method in such protocol since all verifications would be done at runtime. To make it looks cleaner you could just redeclare -applicationDidFinishLaunching: in #interface though you don't really need to, just make one in the #implementaiton is enough.
A delegate may inherit from anything appropriate. It is usually supposed to implement a certain protocol.
A protocol is a way of implementing a formal communication interface between two classes.
However, it is most unlikly that a delegate will inherit from its communication partner class.
With other words: Protocols are often used to overcome the unavailability of multiple inheritance. (Pretty much like interfaces in Java)
Example: A UIViewController subclass' instance controls a view that contains a UITableView. Rather than subclassing the UITableView for the implementation of its look or data, there are two delegates assigned to the table view object. One delegate serves as provider for custom layout (provides items such as the header view) and another (?) delegate provides the data that is being displayed.
Now, this delegate could be any object, inheriting from NSObject and implementing the two protocols. This object cold then be instanciated by the view controller and assigned to the table.
However, it is common practice that the view controller itself serves as delgate for the table(s) that it controls. That is a good pattern but strictly spoken not required. It could be any object.
Now the custom view contoller inherits from UITableViewController (which already implements the protocols and inherits from ViewController) and serves as delgate for the table view. The table view itself could be any subclass of UITableView. (Although this is a bad example here because subclassing UITableView is normally not advisable)
If the delegate does not need to inherit from any class and just implements the protocol, then it shold at least inherit from the cocoa base class NSObject. That ensures that it inherits all the usual capabilites and behaviour of any object. (init method, copy method, description method etc.) That may be required to work properly with other classes of the framework such as beeing used as an object within an NSArray, NSLog etc.

UIViewController and UITableViewController shared logic

I have a class called BaseViewController that inherits from UIViewController and a class called BaseTableViewController that inherits from UITableViewController. Both of these classes have identical shared logic (i.e. rotation handling, helper methods, etc.). Right now the code is simply duplicated in both. I've considered just making a ViewControllerHelper class and exposing it as a public property, but I was wondering if there were any more elegant solutions.
if you don't need any shared ivars/properties, you could write an extension for UIViewController
I'd create a common class like BaseViewController with two initializers:
initWithList: (for table view) and initWithSomething: for simple UIViewController stuff. Each of those initializers would load separate xibs (one with the UITableView as main view, other - as simple UIView).
You'd have the same methods in every instance. And it would only need to implement UITableViewDelegate and UITableViewDataSource protocols.

Is it possible to call a method from the instantiating class?

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.

what is the different between class method and delegate method in iPhone

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.