iOS 单例 class

iOS singleton class

我有一个类型为 UIViewController 的单身人士 class MyController。我可以像 [MyController sharedInstance].view 一样访问视图 属性 并且可以像

那样将其设置为 nil
[MyController sharedInstance].view = nil;

我想限制某人访问视图 属性。我怎么能 stop/restrict 那?

I am using the controller in multiple screens. If the controller is created n number of times, it adds weight to the app. So, to overcome that, I am creating it once and reusing it every where.

您可以为您的 UIViewController 创建一个可跨多个视图使用的基础 class。如果加载新视图,iOS 将正确处理内存管理。旧视图将被卸载

- (void)viewDidDisappear:(BOOL)animated
- (void)viewWillDisappear:(BOOL)animated

将被调用。您可以在那里处理任何多余的数据。

我自己找到了解决方案。我将方法重写为

-(void)setView:(UIView*)view {
    if (view == nil) {
        //ignore - make no change
    }
    else {
        //default performance
        [super setView:view];
    }
}