Objective-C: UIButtons and Event Propagation / for(;;){

UIBUttons that won’t stop event propagation

Today I needed to add a few UIButtons to an interface I had created as a Storyboard. However I am heavily using touchesBegan, touchesMoved, and touchesEnded. UIButtons swallow touch events like that and won’t allow them pass through into my touches<Condition> code. That was a problem for me. You could subclass a UIButton and use it, but I am rather fond of placing some elements myself into a Storyboard. I found a nice solution online. Before the implementation in your view controller, add this code (I think I found it on StackOverflow somewhere):

#pragma mark PassTouch

@interface UIButton (PassTouch)

@implementation UIButton (PassTouch)
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    [super touchesBegan:touches withEvent:event];
    [self.nextResponder touchesBegan:touches withEvent:event];
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    [super touchesMoved:touches withEvent:event];
    [self.nextResponder touchesMoved:touches withEvent:event];
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    [super touchesEnded:touches withEvent:event];
    //[self.nextResponder touchesEnded:touches withEvent:event];

This was a lot easier than subclassing a button, etc. The only thing is that all of your UIButtons are now going to pass events along. I commented that one line out in touchesEnded for the button since that bit was interfering with some of my code. Handy stuff that.

A for loop that won’t quit

I wanted to use a while loop for something (just a random number that wouldn’t be a repeat of the previous number). I stumbled upon some documentation that showed something I haven’t seen before. This is a style thing really as there are other ways to achieve the same and commonly needed results.

    int rndValue;
        rndValue = 0 + arc4random() % ([songsArray count] - 0);
        if(rndValue != currentRandomTrack){
            currentRandomTrack = rndValue;

Check out that for loop. Until that condition is met to break, that for loop will run until someone kills the battery. Just another way to do it, but I hadn’t seen it before so I thought I’d pass it along. Pretty cool looking with the Snidley Whiplash eyes in there.

That’s all – have a great weekend.

Related Posts Plugin for WordPress, Blogger...

One thought on “Objective-C: UIButtons and Event Propagation / for(;;){

  1. Pingback: [BLOCKED BY STBV] Objective-C: UIButtons and Event Propagation / for(;;){

Leave a Reply

Your email address will not be published. Required fields are marked *

7 − = four

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>