我想创建一个带导航控制器的应用程序作为窗口根视图控制器和导航控制器标题视图中的分段控件来切换其根视图控制器
问题:将其添加到nag控制器后,分段控件不存在
代码:
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
FirstViewController * fc = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil];
UINavigationController * nc = [[UINavigationController alloc] initWithRootViewController:fc];
[fc release];
self.window.rootViewController = nc;
NSArray * array = [NSArray arrayWithObjects:@"GPS",@"List",@"Map", nil];
UISegmentedControl * sc = [[UISegmentedControl alloc] initWithItems:array];
sc.frame = CGRectMake(0, 0, 250, 50);
sc.segmentedControlStyle = UISegmentedControlStylePlain;
[nc.navigationItem setTitleView:sc];
[sc release];
[nc release];
[self.window makeKeyAndVisible];
return YES;
}
答案 0 :(得分:0)
您正在为NavigationItem设置titleView,而是为其设置项目。
NSArray * array = [NSArray arrayWithObjects:@"GPS",@"List",@"Map", nil];
UISegmentedControl * sc = [[UISegmentedControl alloc] initWithItems:array];
sc.frame = CGRectMake(0, 0, 250, 50);
sc.segmentedControlStyle = UISegmentedControlStylePlain;
要在标题视图中设置,请使用此行
[ViewController.navigationItem setTitleView:sc];
设置左或右您不能直接将项添加到导航栏,因此使用您的segmentControl创建BarButtonItem
UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:sc];
将其添加到ViewController而不是NavigationController
[ViewController.navigationItem setRightBarButtonItem:segmentItem];