iphone - 我可以覆盖 UIControlEventTouchUpInside 的 UISegmentedControl 吗?

标签 iphone

我有一个 UISegmentedControl,如果您单击已选择的项目,我想用它来执行特定操作。

我的想法基本上是这样的:

- (void)viewDidLoad {
    UISegmentedControl * testButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"one", @"two", nil]];
    [self.view addSubview:testButton];
    [testButton addTarget:self action:@selector(clicked:) forControlEvents:UIControlEventTouchUpInside];
    [super viewDidLoad];
}

-(void) clicked: (id) sender{
    NSLog(@"click");
}

(在clicked:中,我只需做一些检查,看看新选择的索引是否与点击之前旧选择的索引不同)

问题是我似乎无法覆盖 TouchUpInside 控件事件的操作。任何帮助表示赞赏!

-S

最佳答案

您可以使用子类来获得您想要的行为。创建一个具有一个 BOOL ivar 的 UISegmentedControl 子类:

BOOL _actionSent;

然后,在实现中,重写以下两个方法:

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    [super sendAction:action to:target forEvent:event];
    _actionSent = TRUE;
}

- (void) setSelectedSegmentIndex:(NSInteger)toValue {
    _actionSent = FALSE;

    [super setSelectedSegmentIndex:toValue];

    if (!_actionSent) {
        [self sendActionsForControlEvents:UIControlEventValueChanged];
        _actionSent = TRUE;
    }
}

可能还有其他方法,但这对我来说效果很好。我有兴趣了解其他方法。

关于iphone - 我可以覆盖 UIControlEventTouchUpInside 的 UISegmentedControl 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241624/

相关文章:

iphone - 将 NSString 转换为 Int

iphone - Xcode 4 测试构建类冲突

ios - NavigationBar 内容在 popresStatusBarHidden = YES 时从 View 中消失

ios - 在 UITableView 之上为 UIView 添加阴影

iphone - 根据碰撞影响移动图像

iphone - AVAudioPlayer 错误与 m4a 但不是 mp3

ios - iPhone 6 的输入占位符

iphone - CATransition后释放 View Controller : Am I doing this right?

iphone - 在 iPhone 上将经度/纬度转换为 x/y

iphone - iOS UIWebView 中的 Facebook 评论