1

I have a button that I would like both its Touch Up Inside and Touch Up Outside actions to all the same method. I then want the method to sort out which event happened, something like this:

- (IBAction)buttonMethod:(UIButton *)sender forEvent:(UIEvent *)event {
if (event == UIControlEventTouchUpInside)
    NSLog(@"Touch up inside!");

if (event == UIControlEventTouchUpOutside)
    NSLog(@"Touch up outside!");   
}
}

This doesn't seem to work because the touch events seem to inherit from UIControl which isn't in the same universe as UIEvent. Is there any way to tell in my method which action caused the method to be called?

4

1 回答 1

3

将其拆分为两种方法,如果由于某种原因您希望您的逻辑只在一个中传递消息。

- (IBAction)buttonMethod:(UIButton *)sender forEvent:(UIEvent *)event controlEvent:(UIControlEvents)event {
    if (event == UIControlEventTouchUpInside)
        NSLog(@"Touch up inside!");

    if (event == UIControlEventTouchUpOutside)
        NSLog(@"Touch up outside!");   
}

- (IBAction)buttonUpInside:(UIButton *)sender forEvent:(UIEvent *)event {
    [self buttonMethod:sender forEvent:event controlEvent:UIControlTouchUpInside];
}

- (IBAction)buttonUpOutside:(UIButton *)sender forEvent:(UIEvent *)event {
    [self buttonMethod:sender forEvent:event controlEvent:UIControlTouchUpOutside];
}
于 2013-07-03T20:23:13.567 回答