Frage

Disclaimer: I'm new to Interface Builder, so it's possible I'm using it entirely incorrectly.

I've created a NavigationViewController, attached two ViewControllers to it my Storyboard, and added (dragged) a UIButton onto the first VC.

I then created a navigation segue on the UIButton that navigates between the two VCs. This works as expected.

Where things fall apart, is when I try to use a custom subclass of UIButton that I created: the segue is never fired.

  • In my UIButton's property inspector, I set the Custom Class to my custom class name.
  • I added a couple of user defined runtime attributes to the UIButton.

The UIButton header looks like this:

#import <UIKit/UIKit.h>

@interface RSButton : UIButton

@property (nonatomic) CGFloat touchAlpha;

@end

And the UIButton implementation looks like this:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.adjustsImageWhenHighlighted = NO;
    self.highlighted = YES;
    [UIView transitionWithView:self
                      duration:0.04
                       options:UIViewAnimationOptionAllowUserInteraction
                    animations:^{
                        if (self.touchAlpha && self.touchAlpha < 1.0)
                        {
                            self.alpha = self.touchAlpha;
                        }
                    } completion:nil];
}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.highlighted = NO;
    [self resetToDefaultState];
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    self.highlighted = NO;
    [self resetToDefaultState];
}

- (void)resetToDefaultState
{
    [UIView transitionWithView:self
                  duration:0.12
                   options:UIViewAnimationOptionAllowUserInteraction
                animations:^{
                    if (self.alpha < 1)
                    {
                        self.alpha = 1.0;
                    }
                } completion:nil];
}

Interestingly enough, I can see what the problem is: I've overridden the touchesBegan/touchesEnded events, and now the segue stuff isn't firing.

What I can't figure out is how to trigger the attached segue action programmatically?

  • performSegueWithIdentifier isn't available to run on [self], and
  • [self performSelector:@selector(performSegueWithIdentifier:@"segueId" sender:self)]; would suggest I need to know what the segueId is before I fire it.
War es hilfreich?

Lösung

In your method, call:

[super touchesBegan:touches withEvent:event]

So:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event]

    //Your code
}

or:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //Your code

    [super touchesBegan:touches withEvent:event]
}

Do the same in touchEnded etc.

IMPORTANT: Remember to call the method on the superclass using super when you override important and standard methods like those. It's the same that you do when you call init on your subclass:

- (id)init {
    self = [super init];
    if(self) {
        //your code
    }

    return self;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top