English 中文(简体)
如何正确通知某一代表不再需要该案件?
原标题:How to correctly notify a delegate that the instance is no longer needed?

这是我的模式:

1) SpecialView creates a MessageView and holds a strong reference to it.

(2) 用户在电文中利用一个子,导致它消失。 宣传 然后,特别意见告诉它的代表,它完全放弃。

3) 特别 意见发布意见。

问题是:

- (void)fadedOut:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
    [self.delegate messageViewFadedOut:self]; // delegate releases us...
    // self maybe got deallocated... BOOM!
    // now what? method of a zombie returns? stack freaks out?
} // does it even return?

在最后一行Im中,代表立即发出电文。 - fadedOut:完成:正文:由核心估算法StopSelector打回。

我担心的是,“电文”会立即在编造后:结局:完全返回,造成非常新生的随机布道<>。

一度,一位老老老的学者告诉我:“不要切断你再坐的分支”。

因此,为了确保案件在返回这一方法之前就能够存活下来,我在请该代表之前作了保留,然后才:

- (void)fadedOut:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
    //[[self retain] autorelease];
    [self.delegate messageViewFadedOut:self]; // delegate releases us...
}

然而,根据澳大利亚航天中心,再也不允许保留地跳舞(移民工具获得许可),似乎没有任何办法强迫阿农研中心系统做类似的事情。

因此,我提出了这一战略:

- (void)fadedOut:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context {
    [self.delegate performSelector:@selector(messageViewFadedOut:) withObject:self afterDelay:0];
}

希望拖延的表演者能够让这种方法完全返回。 就我所知,拖延了0份,这仍然保证了遴选人在下台坡道时而不是立即进行。

There s a good chance this is bogus as well.

我怎么能够正确地解决一个物体的问题,一个物体要求另一个物体销毁最后提及该物体的可能性,即该物体在向另一个物体发出呼吁的方法之前能够进行处置? 难道可以有一点,如一滴痕迹吗?

我如何在保护儿童权利行动下解决类似问题?

问题回答

为了诚实起见,我认为,你不应试图提出保留,而应确保在代表方法messageViewFadedOut:时,如果发表你自己对你的见解的话,你就不小心。 <<><><> 代码>willFadeOut:<>>>> > 方法可假定其已获得t 地址,但>>>>> 代码>或didFadeOut:<>>>>>/em> 方法应当与所转让物体相一致。

An ideal solution would be for your dealloc method to specifically avoid the nasty random bugs you are dreading, by cancelling any active fadeout animation that is occurring (hold a reference to it somewhere). That way you don t care how or when it gets to dealloc, because dealloc knows to do X to prevent leaving any object state or visual anomalies behind when it dies.

因此,你的解决办法(performSelector:withObject: AfterDelay:) 或可能是全球疾病分类组:

- (void)fadedOut:(NSString*)animationID finished:(NSNumber*)finished context:(void*)context
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 250 * NSEC_PER_MSEC), // 250ms
                   dispatch_get_current_queue(),
                   ^{
                        [self.delegate messageViewFadedOut:self];
                   });
}

此外,ARC可能只是autorelease。 为什么你不试验该法典的一段时期,看什么情况? 在<代码>dealloc上添加一个空白点,看它是否在方法回归之前发出。





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...