Reputation: 7639
I am looking for some additional explanation/insight as to how protocol's and delegation work in Objective-C. I have an app that I am working on, which is using a UINavigationController. There is a main page and a settings page, which will allow a user to input some text that will be used as the main pages' Title. I have everything implemented and working, but I just need some clarifications on how it is working.
Here is an example of how things are set up:
@interface MainPageViewController : UIViewController
@end
@interface MainPageViewController() <SettingsControllerDelegate>
// properties
@end
@implementation MainPageViewController
- (void)methodThatSetsTitle(NSString *)title
{
self.title = title;
}
@end
.....
@protocol SettingsControllerDelegate <NSObject>
{
- (void)methodThatSetsTitle(NSString *)title
}
@interface SettingsViewController
@property (weak, nonatomic) id <SettingsControllerDelegate> delegate;
@end
@interface SettingsViewController ()
// properties that will be used for a text field and holding an NSString
@end
@implementation SettingsViewController
- (void)methodThatPassesStringToDelegateProtocolMethod
{
// Code that will set the property for the NSString title
[self.delegate methodThatSetsTitle:self.titleNameProperty];
}
@end
My question is: How is the NSString title from the SettingsViewController actually getting passed to the MainViewController? My thinking is that the 'delegate' property is declared as a SettingsControllerDelegate so it inherently can hold information from the method's that the protocol has. Then obviously in the MainViewController I call that same protocol method, which will just take the parameter and set the current Navigation title to it. It is just a little confusing as to where that parameter and method information is stored for that other method call to take it. Does every time I call the SettingsViewController method, '- (void)methodThatPassesStringToDelegateProtocolMethod', just call the method in the MainViewController?
(Also in my code I have a prepareForSegue method that set's the SettingViewController.delegate to self.)
Any clarification as to how this information is passed and the details as to how it works would be great! I can understand the complexities, but if you can explain it in a way that is holistic and easy to grasp that'd be great. I can understand memory models and such as well so an explanation as to how this would work in memory would be very useful.
Thank you very much!
Upvotes: 0
Views: 151
Reputation: 2097
I think the main thing that you may be looking for is - what exactly is the delegate property? The declaration
id<SettingsViewControllerDelegate> delegate;
says that you are declaring an object (id) that conforms to the SettingsViewControllerDelegate
protocol - meaning that it implements the methodThatSetsTitle:
method. This can be any object, as long as it conforms to that protocol. So when you do this:
[self.delegate methodThatSetsTitle:self.titleNameProperty];
you are sending a message to that object, whatever it is, to do something with the given NSString.
In your particular case, you are using the Main Page View Controller as the delegate, so the above line of code sends a message from the Settings View Controller to the Main Page View Controller to set its title to the string you are sending as an argument.
In terms of memory, think of this as you would with any other "normal" instance method. The delegate in this case is the Main Page View Controller, so that is presumably on the navigation stack.
Hope this helps!
Upvotes: 3