UIViewController -dealloc method not called

后端 未结 4 1396
半阙折子戏
半阙折子戏 2021-01-03 02:43

I am working with Automatic Reference Counting. I have a custom UIViewController subclass and whenever I call -presentViewController: animated:completion

4条回答
  •  伪装坚强ぢ
    2021-01-03 03:13

    If you want to switch view controllers, and have the one you're switching away from be deallocated, then just switch the root view controller of the window. So, if you're in VC1 and want to go to VC2, then do this in VC1:

    VC2 *vc2 = [[VC2 alloc] init]; // or however else is appropriate to get an instance of this class
    self.view.window.rootViewController = vc2;
    

    If you haven't created any property to point to vc1, then it will be deallocated after making this switch.

    If you want to use a modal presentation or a modal segue (to get the animation when you switch controllers), you can still get the initial controller to be deallocated by switching the root view controller after the presentation from the viewDidAppear method of vc2:

    -(void)viewDidAppear:(BOOL)animated {
        [super viewDidAppear:animated];
        self.view.window.rootViewController = self;
    }
    

提交回复
热议问题