简体   繁体   English

iPhone:将分段控件添加到工具栏,而不是导航控制器中的按钮?

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

im new to iphone programming so if you could help me out I would appreciate it- i have been all over the web and cant find the answer to this. 我是iphone编程的新手,所以如果您能帮助我,我将不胜感激-我遍地都是网络,无法找到答案。

my current setup is like this 我当前的设置是这样的

navigation controller in MainWindow.xib > View in navigation controller in MainWindow.xib calls RootViewController.xib > RootViewController.xib contains a single tableview. MainWindow.xib中的导航控制器> MainWindow.xib中的导航控制器中的视图调用RootViewController.xib> RootViewController.xib包含单个表视图。

i can then load in a toolbar using the following code in RootViewController.m 然后,我可以使用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];

this code works for buttons. 此代码适用于按钮。 but i cannot for the life of me find out how to add a segmented control instead of the buttons. 但是我一生都无法找到如何添加分段控件而不是按钮的方法。 i have tried an array with two segmented controls in it, but then can't add the array to the toolbar. 我已经尝试过在其中具有两个分段控件的数组,但是无法将数组添加到工具栏。

if anyone could let me know some code that will add segmented controls in the same fashion as i have used to add the buttons i would greatly appreciate it. 如果有人能让我知道一些代码,它们将以与我添加按钮相同的方式添加分段控件,我将不胜感激。

thanks, dave. 谢谢,戴夫

The solution to this is to (1) create the UISegmentedControl with all its buttons, etc., and then (2) create a UIBarButtonItem using the initWithCustomView:(UIView *)view initializer and provide the segmented control as the variable to this. 解决方案是(1)使用所有按钮等创建UISegmentedControl ,然后(2)使用initWithCustomView:(UIView *)view初始化程序创建UIBarButtonItem并将分段控件作为变量提供给它。 Then add the Bar Button Item to the Toolbar using an array just like you did in your example code. 然后,像在示例代码中一样,使用数组将Bar Button Item添加到工具栏。

Make sure you set a target and action for your segmented controller, and I recommend setting its style to UISegmentedControlStyleBar . 确保为分段控制器设置目标和操作,建议将其样式设置为UISegmentedControlStyleBar It'll look just like the one at the bottom of the Maps app. 它看起来就像“地图”应用底部的那个。 Hope this was what you are looking for. 希望这就是您想要的。

Here is my code which adds a segmented control to the toolbar of a navigation controller. 这是我的代码,它将代码段控件添加到导航控制器的工具栏。 :

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