【问题标题】:UISegmentedControl does not respond on UIControlEventValueChanged in UIToolbarUISegmentedControl 不响应 UIToolbar 中的 UIControlEventValueChanged
【发布时间】:2012-03-26 10:14:30
【问题描述】:

我用一些项目创建了一个控件

NSArray *items = [NSArray arrayWithObjects: @"First", @"Second", @"Third",  nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];

我在控件中添加了目标

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

比我将分段控件添加到工具栏

[toolbar setItems:[NSArray arrayWithObject:
                          [[[UIBarButtonItem alloc]initWithCustomView:segmentedControl]autorelease]]];

因此,segmentedControl 在用户点击时没有响应。

这里是空的empty方法

-(void) controlTapped:(id)sender {

}

【问题讨论】:

  • 如果你也能写下你的controlTapped:方法可能会有所帮助!
  • 我添加了方法,但它是空的。当我将分段控件添加到导航栏时,此代码有效,但在工具栏中却没有。
  • 它被声明了,它有一些不调用的代码,例如:代码是 NSLog(@"...")。问题不在方法上,因为它在导航栏中起作用。我认为分段控件不想作为 uibarbuttonitem 的自定义视图

标签: objective-c ios uitoolbar uisegmentedcontrol


【解决方案1】:

之前有一个类似的问题是asked and answered(因此可以将UISegmentedControl 添加到工具栏),但我认为这里的问题是您使用了错误的方法来设置项目。我通过apple documentation 并找不到setItems: 方法。请改用setItems:animated:

EDIT- 我通过制作一个示例项目进行了检查,它运行良好。我尝试了您的代码和我的建议,并且两种方式都有效!使用断点确保控件进入(或不进入)controlTapped: 方法。

【讨论】:

  • 我将 setItems 更改为 setItems:animated ,但没有发生任何新情况。我仍然看到段控件,但无法更改选定的值:(
  • 非常感谢)我犯了一个白痴错误,为分段视图设置了错误的框架,导致点击时没有响应。
  • 别担心……这样的错误总是会发生
猜你喜欢
  • 2011-09-21
  • 2010-12-31
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-16
  • 2013-04-21
相关资源
最近更新 更多