簡體   English   中英

iPhone:將分段控件添加到工具欄,而不是導航控制器中的按鈕?

[英]iPhone: adding segmented control to toolbar instead of buttons within navigation controller?

我是iphone編程的新手,所以如果您能幫助我,我將不勝感激-我遍地都是網絡,無法找到答案。

我當前的設置是這樣的

MainWindow.xib中的導航控制器> MainWindow.xib中的導航控制器中的視圖調用RootViewController.xib> RootViewController.xib包含單個表視圖。

然后,我可以使用RootViewController.m中的以下代碼在工具欄中加載

UIBarButtonItem *buttonOne = [[UIBarButtonItem alloc] initWithTitle:@"One" 
     style:UIBarButtonItemStyleBordered target:self action:@selector(buttonOnePushed)];
UIBarButtonItem *buttonTwo = [[UIBarButtonItem alloc] initWithTitle:@"Two" 
     style:UIBarButtonItemStyleBordered target:self action:@selector(buttonTwoPushed)]; 

NSArray *barArray = [NSArray arrayWithObjects: buttonOne, buttonTwo, nil];
[buttonOne release];
[buttonTwo release];

[self setToolbarItems:barArray animated:YES];

[self.navigationController setToolbarHidden:NO animated:YES];

此代碼適用於按鈕。 但是我一生都無法找到如何添加分段控件而不是按鈕的方法。 我已經嘗試過在其中具有兩個分段控件的數組,但是無法將數組添加到工具欄。

如果有人能讓我知道一些代碼,它們將以與我添加按鈕相同的方式添加分段控件,我將不勝感激。

謝謝,戴夫

解決方案是(1)使用所有按鈕等創建UISegmentedControl ,然后(2)使用initWithCustomView:(UIView *)view初始化程序創建UIBarButtonItem並將分段控件作為變量提供給它。 然后,像在示例代碼中一樣,使用數組將Bar Button Item添加到工具欄。

確保為分段控制器設置目標和操作,建議將其樣式設置為UISegmentedControlStyleBar 它看起來就像“地圖”應用底部的那個。 希望這就是您想要的。

這是我的代碼,它將代碼段控件添加到導航控制器的工具欄。

NSArray *segItemsArray = [NSArray arrayWithObjects: @"Settings", @"Templates", @"Notes", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 2;
UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *barArray = [NSArray arrayWithObjects: flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];

[self setToolbarItems:barArray];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM