我使用通知从细节视图控制器在我的应用程序的RootViewController的传递数据。方法工作得很好,直到有一个存储器的警告。

该通知的任何存储器的警告后处理两次。

我将数据传递回RootViewController的当用户选择在DetailViewController一行。该didSelectRowAtIndexPath方法方法被称为仅一次,但是该通知观察者被调用两次!

我应该除去在didReceiveMemoryWarning通知?或者是有一些其他问题与代码?

发布相关的代码

<强> RootViewController的的viewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(rowSelected:) name:@"SelectionNotification" object:nil];

<强> DetailViewController的didSelectRowAtIndexPath方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    NSMutableDictionary *dictionary = [[[NSMutableDictionary alloc] init] autorelease];

    [dictionary setObject:selectedRow forKey:@"row"];
    [[NSNotificationCenter defaultCenter] postNotificationName:kSelectionNotificationName object:self userInfo:dictionary];

    [[self navigationController] popToRootViewControllerAnimated:YES];
}

感谢您的帮助。

有帮助吗?

解决方案

我是很新的iPhone的发展,但我注意到,到目前为止是存储警告后,该didReceiveMemoryWarning方法的默认实现卸载认为,如果是不可见的。

我想你的情况下,根视图控制器是不可见的,并为此卸载。一旦你弹出回到根视图控制器,viewDidLoad方法被再次调用,所以视图控制器实例(其本身没有被卸载,只是该视图)本身再次注册到通知中心。

在解决方案将是在初始化时通知中心来注册,在默认init方法任一之中,或initWithNibName:bundle:方法,或在initWithCoder:方法。

其他提示

如你暗示,如果订阅通知两次,将收到两次。

最有可能你重新实例化对象解除分配和重新订阅通知。

设置您订阅通知断点,你将最有可能打两次。

可以重写访问并从那里通知取消订阅。或者你也可以与志愿做。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top