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.
我正在寻找关于协议和委托如何在Objective-C中工作的一些额外解释/见解。我有一个我正在使用的应用程序,它正在使用UINavigationController。有一个主页面和一个设置页面,允许用户输入一些将用作主页面标题的文本。我已经实现了所有的工作,但我只需要对它的工作方式进行一些澄清。
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?
我的问题是:如何将SettingsViewController中的NSString标题传递给MainViewController?我的想法是'delegate'属性被声明为SettingsControllerDelegate,因此它本身可以保存协议所具有的方法的信息。然后很明显在MainViewController中我调用了相同的协议方法,它只接受参数并将当前的Navigation标题设置为它。关于为其他方法调用存储参数和方法信息的位置,这有点令人困惑。每次我调用SettingsViewController方法,' - (void)methodThatPassesStringToDelegateProtocolMethod'时,只需调用MainViewController中的方法吗?
(Also in my code I have a prepareForSegue method that set's the SettingViewController.delegate to self.)
(同样在我的代码中,我有一个prepareForSegue方法,将SettingViewController.delegate设置为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!
非常感谢你!
1 个解决方案
#1
3
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:
表示你声明一个符合SettingsViewControllerDelegate协议的对象(id) - 意味着它实现了methodThatSetsTitle:方法。这可以是任何对象,只要它符合该协议即可。所以当你这样做时:
[self.delegate methodThatSetsTitle:self.titleNameProperty];
you are sending a message to that object, whatever it is, to do something with the given NSString.
您正在向该对象发送消息,无论它是什么,都可以使用给定的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!
希望这可以帮助!
#1
3
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:
表示你声明一个符合SettingsViewControllerDelegate协议的对象(id) - 意味着它实现了methodThatSetsTitle:方法。这可以是任何对象,只要它符合该协议即可。所以当你这样做时:
[self.delegate methodThatSetsTitle:self.titleNameProperty];
you are sending a message to that object, whatever it is, to do something with the given NSString.
您正在向该对象发送消息,无论它是什么,都可以使用给定的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!
希望这可以帮助!