Segmented navigation bar controls

I am adding a segmented control inside my view controller. My viewdidLoad is as follows

self.navController = [[[UINavigationController alloc] init] autorelease]; UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Image", @""), NSLocalizedString(@"Text", @""), nil]]; [segmentedControl setSelectedSegmentIndex:0]; [segmentedControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar]; segmentedControl.frame = CGRectMake(0, 0, 400, 30); [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged]; self.navigationItem.titleView = segmentedControl; [segmentedControl release]; [self.view addSubview:self.navController.view]; [super viewDidLoad]; 

Only the navigation bar continues to work without any segmented controls inside it. Can someone help and tell me exactly what is wrong here.

+4
source share
1 answer

Your navigation controller starts without a root view controller - you set up the segmented control as the correct presentation of the view controller headers, but you do not give the navigation controller a link to this view controller. You need to initialize it as follows:

 self.navController = [[[UINavigationController alloc] initWithRootViewController:self] autorelease]; 
+3
source

All Articles