A view can only be associated with at most one view controller at a time (UISegmentedControl)

让人想犯罪 __ 提交于 2019-12-08 14:43:43

问题


Hello The error occurs in the emulator on iOS6.

*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'A view can only be associated with at most one view controller at a time! View <UIView: 0xa3ae880; frame = (0 0; 320 367); autoresize = W+H; layer = <CALayer: 0xa3ae8e0>> is associated with <SearchHotelsViewController: 0xa3a6a20>. Clear this association before associating this view with <SecondViewController: 0xa1a9e90>.'

Initialization code

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Поиск туров", @"Выбор отеля", nil]];

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
self.navigationItem.titleView = segmentedControl;

[segmentedControl addTarget:self action:@selector(changeSegments:) forControlEvents:UIControlEventValueChanged];
segmentedControl.selectedSegmentIndex = 0;
self.navigationItem.title = [segmentedControl titleForSegmentAtIndex:segmentedControl.selectedSegmentIndex];
[self setView:searchTours];

SearchHotelsViewController *searchHotelsController = [[SearchHotelsViewController alloc] initWithNibName:@"SearchHotelsViewController" bundle:[NSBundle mainBundle]];
selectHotels = searchHotelsController.view;

App crashes when selected == 1

-(void)changeSegments:(id)sender {
    NSInteger selected = [sender selectedSegmentIndex];
    if (selected == 0) {
        [self setView:searchTours];
    }
    if (selected == 1) {
        [self setView:selectHotels];
    }
    self.navigationItem.title = [sender titleForSegmentAtIndex:selected];
}

I can not understand where the problem is.

SearchHotelsViewController.xib


回答1:


Make sure your ViewController does not contain another view controller object. For example if your main view controller has a tableview, do not put the UITableViewController with in. It this used to pass in iOS 5, but in iOS 6 they will not allow this.




回答2:


I ran into this when I was an idiot and dragged a "UITableViewController" object into the nib to serve as a view instead of a "UITableView". Whoops!




回答3:


I encountered this when I copied/pasted from a Storyboard into a xib file. Recreating the interface from the xib fixed this issue for me.




回答4:


I had a similar problem. Multiple xib files, some worked, some didn't, all had a single UITableView. I had to delete the broken xib files and create new files. After that they all worked.



来源:https://stackoverflow.com/questions/13357788/a-view-can-only-be-associated-with-at-most-one-view-controller-at-a-time-uisegm

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!