且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

UIAlertView或UIActionSheet如何处理在ARC下保留/释放自己?

更新时间:2023-02-17 14:43:19

UIAlertView 创建一个 UIWindow ,它保留。然后,警报视图将自身添加为窗口的子视图,因此窗口将保留警报视图。因此,它创建了一个保持周期,使其和窗口保持活动状态。 UIActionSheet 以相同的方式工作。

UIAlertView creates a UIWindow, which it retains. The alert view then adds itself as a subview of the window, so the window retains the alert view. Thus it creates a retain cycle which keeps both it and its window alive. UIActionSheet works the same way.

如果你需要你的物体留在身边,没有别的东西会保留它,它保留自己是好的。您需要确保有一种定义明确的方法,以便在不再需要时自行释放。例如,如果它正在管理一个窗口,那么它应该在窗口离开屏幕时自行释放。

If you need your object to stay around, and nothing else will retain it, it's fine for it to retain itself. You need to make sure you have a well-defined way to make it release itself when it's no longer needed. For example, if it's managing a window, then it should release itself when it takes the window off the screen.