开发者

event handling in objective c

i try to inspect the low-level of the uiresponder,Because i try to get to the bottom of what happ开发者_StackOverflow社区en when i press a button(uibutton), if there is any other methods that called when a button is clicked or only the IBaction that i had defined.


If you want to know nearly everything that happens on the Cocoa level, create a category for UIResponder like this:

@implementation UIResponder (Inspecting)

- (BOOL)respondsToSelector:(SEL)aSelector {
    NSLog(@"%@ respondsToSelector: %@", [self class], NSStringFromSelector(aSelector));

    return [super respondsToSelector:aSelector];
}

@end

It will print just everything that is invoked on UIResponder subclasses in runtime.


There are several ways you could get more information about that button, and the button press. First of all, if you just need the button, any time you create a method for an interface builder object, you follow this format:

-(IBAction)someMethod:(id)sender

Well, that (id)sender is actually your button. So, you can do pretty much anything you want with it, just give it a cast to stop the compiler from whining at you:

UIButton *myButton = (UIButton *)sender;

If you wanted to be super careful to make sure it actually IS a button before you cast it as such, (id basically means it could be anything) you can check any object's class with the isMemberOfClass method:

if ([sender isMemberOfClass:[UIButton class]]) {

       UIButton *myButton = (UIButton *)sender;

 }

If you need to know which button it is, you could, for instance, give each button a unique tag, and then access that tag like so:

int theTag = [sender tag];

From within your method.

If you need to do more, like know more about the touch itself, you can create your own subclass of UIButton, and then within interface builder, set the class of the button subclass you created to your custom subclass. Then, within that button, override (implement) any of the UIResponder methods that deal with event handling, such as:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   UITouch *aTouch = [touches anyObject];
   [super touchesBegan:touches withEvent:event];
   NSLog(@"Yay! A touch was down, at x: %f and y:%f", [touch locationInView].x, [touch locationInView].y);

    }

For event handling, you can access the UIEvent object supplied, in this case as event, but most often, you're interested in the touches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜