agupta
agupta

Reputation: 403

Segmented Control on Navigation Bar

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

 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 keeps on coming without any segmented control inside it. Can someone help and let me know what exactly is wrong here.

Upvotes: 4

Views: 3397

Answers (1)

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

Your navigation controller is starting out with no root view controller—you're setting the segmented control as your view controller’s title view correctly, but you’re not giving the navigation controller a reference to that view controller. You need to initialize it like this:

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

Upvotes: 3

Related Questions