Objective-C 代表和事件设计(我不明白)
我对 Objective-C 语言还很陌生(不到三个月),但这是我真正需要理解的东西。
假设有一个控制器(在 iOS 环境中)管理来自用户的输入数据的表视图。该表必须具有可编辑单元格和一些功能,以使值选择更容易,例如显示带有字段可能值的弹出窗口的按钮。
假设有一个字段存储国家名称。弹出窗口首先显示大陆列表;当用户选择一个大陆时,弹出窗口的控制器必须显示预览所选大陆的国家/地区。
现在,这个弹出窗口出现在应用程序的许多地方,因此如果我可以封装它以供以后使用,那就太好了。我对这个弹出窗口的期望是这样的:
...
@protocol MyPopoverDelegate<NSObject> {
-(void)didSelectCountry:(NSString *)countryName;
{
...
MyPopoverController *dataSelector = [[MyPopoverController] alloc] init];
dataSelector.dataType = CountryDataType;
dataSelector.delegate = self;
[dataSelector show];
[dataSelector release];
...
这里的问题是行 [dataSelector release]
因为管理弹出窗口的代码必须保持活动状态,直到选择国家/地区。这意味着 dataSelector 变量必须是调用者类的属性,这很糟糕。
那么问题是: 我如何组织这样的情况以获得可重用的控制器?
谢谢
在 vodkhang 回答后编辑:
好的,这是一个很好的问题,但是 dataSelector
仍然是一个属性。 如果我这样做怎么办:
@implementation MyPopoverController
- (id)init {
...
[self retain];
...
}
- (void)popoverControllerDidDismissPopover: (UIPopoverController *)popoverController {
...
[delegate didFinishSelectingCountry:countryName];
[self release];
}
@end
我从来没有在 Objective-C 中看到过这种行为,我觉得这不是这个想法。 为什么是错的?
I'm pretty new to the objective-c language (less than three months) but it is something that i really need to understand.
Suppose there is a controller (in a iOS environment) that manages a table view for input data from the user. The table must have editable cells and some features to make the value selection easier, for example a button that shows a popover with the possible values for a field.
Suppose there is a field to store country names. The popover first shows a list of continents; when the user selects a continent, the controller of the popover must show the countries of the previews selected continent.
Now, this popover appears in many places in the app so it will be nice if I can encapsulate it for later use. What i will expect for this popover is something like this:
...
@protocol MyPopoverDelegate<NSObject> {
-(void)didSelectCountry:(NSString *)countryName;
{
...
MyPopoverController *dataSelector = [[MyPopoverController] alloc] init];
dataSelector.dataType = CountryDataType;
dataSelector.delegate = self;
[dataSelector show];
[dataSelector release];
...
The problem here is the line [dataSelector release]
because the code for managing the popover must stay alive until the country is selected. That's means the dataSelector
variable must be a property of the caller class and that sucks.
The question then is:
How can i organize situations like this to have a reusable controller?
Thanks
Edited after vodkhang answer:
Ok, that's a good one, but dataSelector
still is a property.
What if i do:
@implementation MyPopoverController
- (id)init {
...
[self retain];
...
}
- (void)popoverControllerDidDismissPopover: (UIPopoverController *)popoverController {
...
[delegate didFinishSelectingCountry:countryName];
[self release];
}
@end
I never see this behavior in objective-c, i feel that this is not the idea.
Why is it wrong?.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以为委托方法做的一种方法是:
这种方法经常被使用,如 NSUrlConnection、UIImagePickerController
One of the way you can do for delegate method is to have:
This way is used a lot like NSUrlConnection, UIImagePickerController
如果您希望从视图层次结构中的任何位置在整个应用程序中可重用一些唯一的对象,您可以将其作为应用程序委托的属性,并让应用程序委托拥有它(在活动时保留它,在内存警告期间释放它等。 )。
如果您将代码移植到垃圾收集环境中,自保留对象最终可能会遇到问题。
If you want some unique object reusable across an entire app from anywhere in the view hierarchy, you can make it a property of the app delegate, and let the app delegate own it (retain it when live, release it during memory warnings, etc.).
A self retained object may eventually run into problems if you ever port your code to a garbage collected environment.