2013-04-09 73 views
1
的RootViewController的

我想創建一個應用程序與導航控制器作爲窗口根視圖控制器和在導航控制器的標題視圖分段控制以切換其根視圖控制器的UINavigationController與UISegmentedControl在AppDelegate中切換的UINavigationController

問題:分段控制不將其添加到嘮叨控制器後本

代碼:

(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

嘗試將其添加到fc.navigationItem,然後您可能需要將此分段控制視圖添加到呈現的每個視圖控制器的所有導航項 – 2013-04-09 10:50:21

回答

0

您正在爲NavigationItem代替設置項對於i設定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]; 

要設置左或右,您不能直接添加項目到導航條,所以創建BarButtonItem與segmentControl

UIBarButtonItem *segmentItem = [[UIBarButtonItem alloc] initWithCustomView:sc]; 

將其添加到ViewController不要導航控制器

[ViewController.navigationItem setRightBarButtonItem:segmentItem]; 
相關問題