1
votes

I have an app with two view controllers. ViewControllerA is a blank view with a tap gesture assigned which allows the user to tab on the view and create a UITextView at the point of the tap. The user can create as many UITextViews as they wish and they are added then programmatically to the view (ViewControllerA) as sub views.

There is also a button which allows the user to change the text font and styling. This triggers a Segue to the second view controller ViewControllerB which then allows the user to set Font, Text Size, Color etc. Once completed the user clicks the DONE button on ViewControllerB and another Segue switches back to the initial view (ViewControllerA).

This all works fine. Except when the user switches back to the initial view (ViewControllerA) from ViewControllerB the view is reloaded from the storyboard and the sub views I have added programmatically are gone.

In view (ViewControllerA) ViewDidLoad and ViewWillAppear are firing just fine so the problem seems to be the initial view is released when the first Segue fires and then recreated from the storyboard on the transition back but the subviews are of course not included as they are not in the storyboard since I added them programmatically.

Any suggestions for a best practice on how to solve this so that the subviews are recreated also when the main view (ViewControllerA) reloads?

Many thanks for any suggestions!

2

2 Answers

1
votes

From the question it sounds like you had a segue to the text styles view, then another segue "back to the original" - it doesn't work like that, segues always make new instances of the destination VC. You should have had a modal segue to the text styles view, then dismissed the modal view controller - this would return to your original instance.

1
votes

Just for the record, I solved this as follows in case anyone else needs a solution.

I created a subview in ViewControllerA which is the size of the main view excluding the Toolbar. I call this canvasView. Then I add all of my ImageViews and TextViews to this canvas view.

Then in ViewControllerA viewWillDisappear I archive the canvasView and all of its subviews to a file like this.

    NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Canvas.archive"];
BOOL result = [NSKeyedArchiver archiveRootObject:_canvasView
                                     toFile:archivePath];
if (!result) {
    NSLog(@"Archive failed to archivePath %@",archivePath);
}

Then in ViewControllerA viewWillAppear I check if there is an existing archive and if so reload it which loads the sub views in the correct order. Otherwise I create an empty canvasView like this.

    // If the collageView already exists then restore it from the achive, otherwise initialize a new one.

NSString *archivePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"Canvas.archive"];

_canvasView = [NSKeyedUnarchiver unarchiveObjectWithFile:archivePath];

if (_canvasView) {
    // Restore existing canvasView
    [_backgroundView addSubview:_canvasView];
} else {
    // Initialize a new canvasView
    _canvasView = [[UIScrollView alloc] initWithFrame:CGRectMake(_backgroundView.frame.origin.x, 
                                                           _backgroundView.frame.origin.y, 
                                                           _backgroundView.frame.size.width, 
                                                           _backgroundView.frame.size.height)];
    [_backgroundView addSubview:_canvasView];
}