Use of Delegate in Objective-C [duplicate] - ios

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does a delegate work in Objective-C?
Can Someone Help Me To Understand What Is This "Delegate"?
What is Use of Delegate method in Objective-C
if I do same functionality using subclass method why we use delegate method in Objective-C

You may want to implement more delegates, while obj-c supports only single inheritance.
Anyway, always choose composition and interface/delegates over inheritance.

Think of a delegate method as a method external to the class. Any class can then become useful (become the delegate of) the class by complying with the delegate protocol and implementing the required delegate method(s). Delegation adds flexibility without the restrictions inherent to the parent class being carried over by subclassing.

Related

why can NSObject class be delegate to UIImagePickerController for instance? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
i need explanation of how could NSObject Class be a delegate to any controller although NSObject is not a controller !?
A delegate is just an implementation of Delegate design pattern. In Cocoa classes it is implemented using Protocols - any class that implements a specific protocol can be a delegate. For example, let's look at the definition of delegate property in UITableView class (in Objective-C, because it's better at showing distinction between classes and protocols):
#property(nonatomic, weak) id<UITableViewDelegate> delegate;
In Objective-C, id is a universal pointer - a pointer to object of any class. You can see that UITableView expects it's delegate to be of any class, but a class that implements UITableViewDelegate protocol
The only requirement for a class to be a delegate for a particular class is to implement the protocol required. It doesn't depend upon its inheritance chain. It could be uiviewcontroller, uitableviewcontroller or simply the NSObject.
If your class confirms to the required protocol then its a fair candidate to be the delegate.
An object can be a delegate without being a controller.
Being a delegate just means that you agree to respond to a set of methods. It doesnt matter what kind of object you are, as long as you understand the methods in the delegate protocol, you can be a delegate.
Say, for example, you have a central table view manager object that knows about mulitple table views throughout your UI. You could make that table view manager the delegate of all the table views in your app. (I don’t know if this would make sense, but you could certainly do it if you had a reason to do so.)

Binding triggered actions to functions without subclassing

For analytic purposes, I would like to do the following:
When a UIVIewController's viewDidLoad() is triggered, I would like to trigger a custom function vcWasLoaded(vc:UIViewController).
Similarly, when a UIButton is tapped, I would like to trigger a custom function btnWasTapped(bt:UIButton).
I would like to achieve the above without subclasses. Anyway to achieve the above using protocols, extensions, or reactive frameworks?
Method swizzling is the only thing I can think of that you could use that would let you do this without subclassing. You'd replace the implementation of viewDidLoad, and one of the lower-level button methods, and then call the original implementation in yours. (I've only dabbled in method swizzling, and it was many years ago, before Swift existed. I don't know much about Objective-C method swizzling, and know exactly zero about method swizzling in Swift.)
This would be much simpler and cleaner if you created a subclass of UIViewController and made it the base class of all of your view controllers.

Composition vs inheritance for iOS base view controller

In general, I've heard its better to use composition than inheritance, but it's not always clear to me how to do that. I want to create some functionality that's common across all of my view controllers (I want to listen for an NSNotification, and call a method if it receives the notif).
My idea is to create a BaseViewController that each UIViewController extends from. I'd love to solve this any other way than inheritance for many reasons
Some view controllers extend UIViewController and some extend UITableViewController
If I write it in swift, objc view controllers can't subclass it
Normal reasons for comp over inheritance - easier to understand the behavior
My question is - how do I accomplish this without copy and pasting a ton of code into each viewcontroller? I could obviously insert a line into each view controllers viewDidLoad method, to add a listener, and into each view controllers delloc, but I'd really rather avoid this. Is there other techniques that could make this cleaner?
In Swift 2.0 you can use protocol with default method implementations. But in this case is inheritance the best approach for me.
According to me, it will be better to implement some functionality which is common across all ViewControllers by using Singleton Class.
Create a simple swift file having Singleton class in it and implement common functions in it which you want to access anywhere. Create a shared instance for that class and by using this shared instance, you can call any function in any ViewController of your application. So that you can reuse the code without copy-paste.

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

Resources