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

发布于 2024-08-07 02:25:22 字数 985 浏览 2 评论 0原文

我对 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];

该代码适用于按钮。但我一生都无法找到如何添加分段控件而不是按钮。我尝试了一个包含两个分段控件的数组,但无法将数组添加到工具栏。

如果有人能让我知道一些代码,这些代码将以与我添加按钮相同的方式添加分段控件,我将不胜感激。

谢谢,戴夫。

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.

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.

i can then load in a toolbar using the following code in 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

流星番茄 2024-08-14 02:25:22

解决方案是 (1) 创建 UISegmentedControl 及其所有按钮等,然后 (2) 使用 initWithCustomView:( 创建 UIBarButtonItem UIView *)view 初始值设定项并提供分段控件作为变量。然后使用数组将 Bar Button Item 添加到工具栏,就像您在示例代码中所做的那样。

确保为分段控制器设置目标和操作,我建议将其样式设置为 UISegmentedControlStyleBar。它看起来就像地图应用程序底部的那个一样。希望这就是您正在寻找的。

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. Then add the Bar Button Item to the Toolbar using an array just like you did in your example code.

Make sure you set a target and action for your segmented controller, and I recommend setting its style to UISegmentedControlStyleBar. It'll look just like the one at the bottom of the Maps app. Hope this was what you are looking for.

黑色毁心梦 2024-08-14 02:25:22

这是我的代码,它将分段控件添加到导航控制器的工具栏。
:

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];

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文