【问题标题】:How do I create a UIBarButtonItem with multiple "sections"?如何创建具有多个“部分”的 UIBarButtonItem?
【发布时间】:2009-05-14 16:11:52
【问题描述】:

我想在我的 iPhone 应用程序上创建一个 UIBarButtonItem,它有两个“部分”。基本上,我希望拥有与 YouTube iPhone 应用的“观看次数最多”部分中的“今天”、“本周”和“全部”按钮等效的功能。

这个功能看起来不像是通过多个 UIBarButtonItems 完成的,因为一次只能选择三个“部分”中的一个。有谁知道这是怎么做到的?

【问题讨论】:

    标签: iphone objective-c cocoa-touch


    【解决方案1】:

    您看到的是 UISegmentedControl。这很容易设置。 YouTube 正在做的事情(可能)是这样的:

    NSArray * items = [[NSArray alloc] initWithObjects: NSLocalizedString(@"Today", @""),
                          NSLocalizedString(@"This Week", @""),
                          NSLocalizedString(@"All", @""), nil];
    UISegmentedControl * switcher = [[UISegmentedControl alloc] initWithItems: items];
    [items release];
    
    // setup the switcher: correct UI style, tint, etc.
    switcher.style = UISegmentedControlStyleBar;
    switcher.tint = self.navigationController.navigationBar.tintColor; // unnecessary?
    
    // set the target function -- needs to check selectedIndex property
    [switcher addTarget: self 
                 action: @selector(switcherTapped:)
       forControlEvents: UIControlEventValueChanged];
    
    // set default selection
    switcher.selectedSegmentIndex = 0;
    
    // set the switcher as a custom view to use in place of the normal title
    self.navigationItem.titleView = switcher;
    [switcher release];
    

    【讨论】:

      【解决方案2】:

      其实我觉得他是在描述一个UISegmentedControl,可以像这样添加到当前视图的导航栏上:

      UISegmentedControl *segmentedControl = ... 
      self.navigationItem.titleView = segmentedControl; 
      [segmentedControl release];
      

      您可以像这样设置 UISegmentedControl ("Today, Last Week, All") 的段(这也设置了控件值更改时的回调):

      NSArray *sampleArray = --makeAnArrayHere--;
      UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] 
          initWithItems:sampleArray];
      [segmentedControl addTarget:self action:
          @selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
      

      我没有从工作项目中复制此内容,因此可能存在一些小的语法错误,但这应该会引导您朝着正确的方向前进。

      【讨论】:

        【解决方案3】:

        看看 YouTube 应用,我认为您实际上会想要使用 UISegmentedControl。

        【讨论】:

          【解决方案4】:

          您正在寻找的是带有UITabBarItems 的UITabBar 控件。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-08-17
            • 1970-01-01
            • 2020-09-04
            • 1970-01-01
            • 1970-01-01
            • 2019-05-14
            相关资源
            最近更新 更多