UIPopoverController内存问题

发布于 2024-12-03 04:57:52 字数 1651 浏览 4 评论 0原文

我正在创建一个 UIPopoverController 并将“Editor1”设置为内容视图控制器。 当调用者收到 didDismissPopover 时,我将释放 UIPopoverController。 这是代码:

- (IBAction)open1:(id)sender {

Editor1 *editor = [[Editor1 alloc] initWithNibName:@"Editor1" bundle:nil];
_popoverController = [[UIPopoverController alloc] initWithContentViewController:editor];
_popoverController.delegate = self;
[editor release];

[self.popOverController presentPopoverFromRect:self.open1Button.bounds inView:self.open1Button permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popoverControllerShouldDismissPopover");
    return YES;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
     NSLog(@"popvoerControllerDidDismissPopover");
    [_popoverController release];
}

在我的编辑器中,我有一个 UITextField,用户可以在其中更改文本,并在收到消息“editingDidEnd”时保存它。

- (IBAction)editingDidEnd:(id)sender {
   NSLog(@"Editing did End");
   // SAVE PROCEDURE
}

我的问题涉及调用方法的顺序。 顺序是:

2011-09-07 12:35:21.628 iosTest[1967:b603] popoverControllerShouldDismissPopover
2011-09-07 12:35:21.629 iosTest[1967:b603] popvoerControllerDidDismissPopover 
2011-09-07 12:35:21.983 iosTest[1967:b603] Editing did End
2011-09-07 12:35:21.985 iosTest[1967:b603] viewWill Disappear

正如您所看到的,popoverControllerDidDismissPopover 在 editingDidEnd: 之前被调用,所以这意味着我在执行保存过程之前释放了弹出窗口。这可能会给我带来崩溃问题。

另外,在我的保存过程中,在某些情况下我需要要求用户确认。我为此使用 UIAlertView

您有什么建议吗?

I'm creating a UIPopoverController and setting "Editor1" as the content view controller.
When the caller receives the didDismissPopover I'm releasing the UIPopoverController.
This is the code:

- (IBAction)open1:(id)sender {

Editor1 *editor = [[Editor1 alloc] initWithNibName:@"Editor1" bundle:nil];
_popoverController = [[UIPopoverController alloc] initWithContentViewController:editor];
_popoverController.delegate = self;
[editor release];

[self.popOverController presentPopoverFromRect:self.open1Button.bounds inView:self.open1Button permittedArrowDirections:UIPopoverArrowDirectionAny animated:NO];
}

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popoverControllerShouldDismissPopover");
    return YES;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
     NSLog(@"popvoerControllerDidDismissPopover");
    [_popoverController release];
}

In my editor I have a UITextField where the user changes text and I save it when I get the message "editingDidEnd"

- (IBAction)editingDidEnd:(id)sender {
   NSLog(@"Editing did End");
   // SAVE PROCEDURE
}

My question regards the order in which the methods get called.
The order is:

2011-09-07 12:35:21.628 iosTest[1967:b603] popoverControllerShouldDismissPopover
2011-09-07 12:35:21.629 iosTest[1967:b603] popvoerControllerDidDismissPopover 
2011-09-07 12:35:21.983 iosTest[1967:b603] Editing did End
2011-09-07 12:35:21.985 iosTest[1967:b603] viewWill Disappear

As you can see the popoverControllerDidDismissPopover gets called before editingDidEnd:, so this means I'm releasing the popover before I do my save procedure. This could bring me a crash problem.

Also, in my save procedure I need to ask the user for confirmation in some cases. I'm using a UIAlertView for this.

Do you have any recommendations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

对你再特殊 2024-12-10 04:57:52

通常视图表现良好,并且在离开屏幕后不会发送事件。您可以通过启用僵尸来检查潜在问题(设置环境变量 NSZombieEnabled=YES)。

如果发生崩溃,修复它的正确位置是 -[Editor1 dealloc] (也可能是 -viewDidUnload):只需执行 textField.delegate = nil 并且您应该停止接收回调。这通常是不必要的,除非 Web 视图和滚动视图似乎有问题(即使 VC 不在屏幕上,滚动动画也会继续)。

在您的情况下,您可能可以在 -popoverControllerShouldDismissPopover: 中进行保存,如果您需要显示 UIAlertView (并在按下按钮时关闭弹出窗口),则返回 NO。

Usually views are well-behaved and don't send events after they're off-screen. You can check for potential problems by enabling zombies (set the environment variable NSZombieEnabled=YES).

If there is a crash, the correct place to fix it is in -[Editor1 dealloc] (and possibly -viewDidUnload): just do textField.delegate = nil and you should stop receiving callbacks. This is not usually necessary except for web views and scroll views where it seems to be problematic (the scroll animation continues even if the VC is off-screen).

In your case, you can probably make saving happen in -popoverControllerShouldDismissPopover:, returning NO if you need to display a UIAlertView (and dismissing the popover when the button is pressed).

自我难过 2024-12-10 04:57:52

看起来, _popoverController 是实例属性。在这种情况下,您可以在父控制器的 viewDidUnload 方法中释放它。

it seems, that _popoverController is the instance-property. in this case you can release it in viewDidUnload method of parent-controller.

灯角 2024-12-10 04:57:52

为什么不使用 UITextFieldDelegate 协议?用法:

aTextField.delegate = self;

(...)

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"Editing did End");
    // SAVE PROCEDURE
}

阅读文档更多信息。

Why don't you use UITextFieldDelegate protocol? Usage:

aTextField.delegate = self;

(...)

- (void)textFieldDidEndEditing:(UITextField *)textField {
    NSLog(@"Editing did End");
    // SAVE PROCEDURE
}

Read the documentation for more info.

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