UIButtonBarItem 中的 UISegmentedControl

Posted

技术标签:

【中文标题】UIButtonBarItem 中的 UISegmentedControl【英文标题】:UISegmentedControl in UIButtonBarItem 【发布时间】:2013-06-24 14:09:33 【问题描述】:

我有一个想要出现在 UIToolbar 中的 UISegmentedControl。它出现了,但单击它不会调用它应该调用的方法。我错过了什么吗?

代码:

-(void)setupToolbars
    NSArray *segItemsArray = [NSArray arrayWithObjects: @"List", @"Day", @"Month", nil];
    segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
    segmentedControl.selectedSegmentIndex = 2;
    [segmentedControl addTarget:self action:@selector(changeView) forControlEvents:UIControlEventTouchUpInside];//this line should make the segmented control call the correct method
    UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
    NSArray *barArray = [NSArray arrayWithObjects: todayButtonItem,flexibleSpace, segmentedControlButtonItem, flexibleSpace, nil];
    [bottomToolbar setItems:barArray];

-(void)changeView
    NSLog(@"changeView");
    ...

【问题讨论】:

【参考方案1】:

您想使用UIControlEventValueChanged 事件,而不是UIControlEventTouchUpInside 事件。

【讨论】:

【参考方案2】:

只是建立在 rmaddy 所说的之上。我还建议使用 UIControlEventValueChanged 事件。

[segmentedControl addTarget:self action:@selector(didChangeSegmentControl:) forControlEvents:UIControlEventValueChanged];


-(void)didChangeSegmentControl:(UISegmentedControl*) control

    if (control.selectedSegmentIndex ==0 )
     
          //...
    
  

【讨论】:

以上是关于UIButtonBarItem 中的 UISegmentedControl的主要内容,如果未能解决你的问题,请参考以下文章

粗体文本辅助功能选项隐藏 UIButtonBarItem 图形

iOS Xcode 中的 UIViewController 中的 UIView 中的 UITableView

如何用ruby中的数组中的元素替换字符串中的单词?

如何将视图中的 javascript 代码中的对象列表传递给控制器​​中的操作方法

如何从账户 A 中的 Lambda(VPC 中的 Lambda)调用账户 B 中的 AWS Lambda 函数(VPC 中的这个 Lambda)

我可以在 apatch 中的 php 文件中播放位于硬盘中的文件路径中的视频吗?