1

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!

4

2 回答 2

1

从这个问题听起来你对文本样式视图有一个转场,然后是另一个转场“回到原来的” - 它不像那样工作,转场总是会创建目标 VC 的新实例。您应该对文本样式视图进行模态搜索,然后关闭模态视图控制器 - 这将返回到您的原始实例。

于 2012-06-16T12:17:59.553 回答
1

只是为了记录,我解决了这个问题,以防其他人需要解决方案。

我在 ViewControllerA 中创建了一个子视图,它是主视图的大小,不包括工具栏。我称之为canvasView。然后我将我所有的 ImageViews 和 TextViews 添加到这个画布视图中。

然后在 ViewControllerA viewWillDisappear 中,我将 canvasView 及其所有子视图存档到这样的文件中。

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

然后在 ViewControllerA viewWillAppear 中检查是否存在现有存档,如果存在则重新加载它以正确顺序加载子视图。否则,我会像这样创建一个空的 canvasView。

    // 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];
}
于 2012-06-16T10:40:56.703 回答