ios – UIViewControllerHierarchyInconsistency

前端之家收集整理的这篇文章主要介绍了ios – UIViewControllerHierarchyInconsistency前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试构建我的应用程序,在某种程度上我推一个UIViewController,然后我得到这个错误.我不知道为什么

‘UIViewControllerHierarchyInconsistency’,reason: ‘A view can only be
associated with at most one view controller at a time! View > is associated with . Clear this association before associating this view with
.’

PageViewController *viewController;

viewController = [[PageViewController alloc] initWithManagedObjectContext:managedObjectContext];
dataSource = [[PagesDataSource alloc] initWithManagedObjectContext:managedObjectContext];

PVPage *selectedPage = [[dataSource pages] objectAtIndex:itemIndex];
[viewController setRepresentedPage:selectedPage];

PageFlipperAppDelegate *appDelegate = (PageFlipperAppDelegate *)[[UIApplication sharedApplication] delegate];
[(UINavigationController *)[[appDelegate window] rootViewController] setToolbarHidden:YES animated:YES];
[(UINavigationController *)[[appDelegate window] rootViewController] pushViewController:viewController animated:YES];

在我的pageViewController ……………….

- (id)initWithManagedObjectContext:(NSManagedObjectContext *)initManagedObjectContext
{
    if ((self = [super initWithNibName:@"PageView" bundle:nil]))
    {
        [self setManagedObjectContext:initManagedObjectContext];
        dataSource = [[PagesDataSource alloc] initWithManagedObjectContext:[self managedObjectContext]];
    }
    return self;
}

解决方法

当我想要一个设置滚动视图显示在一个popover时,我出现了同样的错误.

这是我的原始代码,有关我改变以解决它的意见:

SettingsViewController *settingsViewController;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];    
settingsViewController = [storyboard instantiateViewControllerWithIdentifier:@"Settings"];

CGRect contentRect = CGRectMake(10,10,320,700);

// This entire object got deleted in the fixed version
UIViewController *popoverContent = [[UIViewController alloc] init];
popoverContent.view = settingsViewController.view;
popoverContent.contentSizeForViewInPopover = contentRect.size;

// Instead of popoverContent I just put the settingsViewController in directly.
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];

[popoverController presentPopoverFromRect:[sender frame]
                                   inView:(UIButton*)sender
                 permittedArrowDirections:UIPopoverArrowDirectionLeft
                                 animated:YES];

行popoverContent.view = settingsViewController.view是导致崩溃的原因(没有它,我的popover当然是空的).从UIViewControllerHierarchyInconsistency错误的角度来看,我不应该重新分配它,然后将其添加到另一个视图控制器.

在你的情况下,我猜想pushViewController或代码中的其他地方发生了类似的事情.可以这样吗?

原文链接:https://www.f2er.com/iOS/336999.html

猜你在找的iOS相关文章