Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

Hello The error occurs in the simulator on iOS 6.

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 is true

-(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 cannot understand where the problem is.

SearchHotelsViewController.xib

SearchHotelsViewController.xib

like image 541
EndyVelvet Avatar asked Nov 13 '12 08:11

EndyVelvet


People also ask

What is the difference between View and Controller?

The view renders presentation of the model in a particular format. The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.

What is a view controller in Swift?

A view controller manages a single root view, which may itself contain any number of subviews. User interactions with that view hierarchy are handled by your view controller, which coordinates with other objects of your app as needed. Every app has at least one view controller whose content fills the main window.


1 Answers

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.

like image 86
mskw Avatar answered Oct 13 '22 09:10

mskw