View controller getting duplicate instantiation

后端 未结 2 1568
迷失自我
迷失自我 2021-01-21 14:54

This should be pretty straight forward.

When I use the navigation controller to segue from my root view to my 2nd view, the 2nd view loads fine. The 2nd view creates a t

相关标签:
2条回答
  • 2021-01-21 15:17

    Off the top of my head: In your second view controllers deallocate method, you should stop the timer of the "updateData", as it is not properly deallocated. Try that!

    [self.timer invalidate];
    
    0 讨论(0)
  • 2021-01-21 15:30

    It's not necessarily true that persistent objects shouldn't be properties of view controller. You can create a property for your second controller (in the root view controller), and only instantiate it the first time you push it. Because you have a strong pointer to it, it won't be deallocated when you go back to the first controller, and your timer will continue to operate.

    - (IBAction)goToSecondView {
        if (!self.secondViewController) { // secondViewController is a strong property
            self.secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Second"]; 
        }
        [self.navigationController pushViewController:self.secondViewController animated:YES];
    }
    
    0 讨论(0)
提交回复
热议问题