Objective-C 代表和事件设计(我不明白)

发布于 2024-09-26 07:24:19 字数 1202 浏览 4 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

游魂 2024-10-03 07:24:19

您可以为委托方法做的一种方法是:

MyPopOverDelegate
    - (void)didFinishSelectingCountry:(NSString *)countryName popOver:(MyPopOver *)popOver;
    Caller.m

// the caller
- (void)viewDidLoad {
MyPopoverController *dataSelector = [[MyPopoverController] alloc] init];
dataSelector.dataType = CountryDataType;
dataSelector.delegate = self;
[dataSelector show];

}

- (void)didFinishSelectingCountry:(NSString *)countryName popOver:(MyPopOver *)popOver {
   // finish stuff
   [popOver release];
}

这种方法经常被使用,如 NSUrlConnection、UIImagePickerController

One of the way you can do for delegate method is to have:

MyPopOverDelegate
    - (void)didFinishSelectingCountry:(NSString *)countryName popOver:(MyPopOver *)popOver;
    Caller.m

// the caller
- (void)viewDidLoad {
MyPopoverController *dataSelector = [[MyPopoverController] alloc] init];
dataSelector.dataType = CountryDataType;
dataSelector.delegate = self;
[dataSelector show];

}

- (void)didFinishSelectingCountry:(NSString *)countryName popOver:(MyPopOver *)popOver {
   // finish stuff
   [popOver release];
}

This way is used a lot like NSUrlConnection, UIImagePickerController

尹雨沫 2024-10-03 07:24:19

如果您希望从视图层次结构中的任何位置在整个应用程序中可重用一些唯一的对象,您可以将其作为应用程序委托的属性,并让应用程序委托拥有它(在活动时保留它,在内存警告期间释放它等。 )。

如果您将代码移植到垃圾收集环境中,自保留对象最终可能会遇到问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文