View controller getting duplicate instantiation

后端 未结 2 1569
迷失自我
迷失自我 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: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];
    }
    

提交回复
热议问题