Segmented Control on Navigation Bar
I am adding a segmented control inside my view controller.My viewdidLoad is as follow
开发者_StackOverflow社区 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.
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];
精彩评论