【问题标题】:iPhone: adding segmented control to toolbar instead of buttons within navigation controller?iPhone:将分段控件添加到工具栏而不是导航控制器中的按钮?
【发布时间】:2009-10-02 12:12:57
【问题描述】:

我是 iphone 编程新手,所以如果你能帮助我,我将不胜感激 - 我已经在网上找到了答案。

我现在的设置是这样的

MainWindow.xib 中的导航控制器 > MainWindow.xib 中导航控制器中的视图调用 RootViewController.xib > RootViewController.xib 包含单个 tableview。

然后我可以使用 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];

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

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

谢谢,戴夫。

【问题讨论】:

    标签: iphone uitableview navigation controller toolbar


    【解决方案1】:

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

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

    【讨论】:

    • 如何获取在 segmentedController 选择器中选择了哪个段?
    • 检查其 selectedSegmentIndex 属性
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-01
      • 1970-01-01
      相关资源
      最近更新 更多