繁体   English   中英

使用Interface Builder连接到父视图控制器的IBAction的UIButton的子类?

[英]Subclass of UIButton connecting to IBAction of parent View Controller using Interface Builder?

免责声明:我是Interface Builder的新手,所以有可能我完全不正确地使用了它。

我创建了一个NavigationViewController ,在我的Storyboard上附加了两个ViewControllers ,然后将UIButton添加(拖放)到第一个VC上。

然后,我在UIButton上创建了一个导航序列,可以在两个VC之间进行导航。 这按预期工作。

当我尝试使用我创建的UIButton的自定义子类时,事情就变得不那么重要了:永远不会触发segue。

  • UIButton的属性检查器中,将“自定义类”设置为我的自定义类名称。
  • 我向UIButton添加了两个用户定义的运行时属性。

UIButton标头如下所示:

#import <UIKit/UIKit.h>

@interface RSButton : UIButton

@property (nonatomic) CGFloat touchAlpha;

@end

UIButton实现如下所示:

- (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];
}

有趣的是,我可以看到问题出在哪里:我已经覆盖了touchesBegan/touchesEnded事件,现在segue的东西没有触发。

我不知道是如何以编程方式触发附加的segue动作的?

  • performSegueWithIdentifier无法在[self]上运行,并且
  • [self performSelector:@selector(performSegueWithIdentifier:@"segueId" sender:self)]; 建议我在启动前需要知道segueId是什么。

在您的方法中,调用:

[super touchesBegan:touches withEvent:event]

所以:

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

    //Your code
}

要么:

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

    [super touchesBegan:touches withEvent:event]
}

在touchEnded等中执行相同的操作。

重要说明:当您覆盖诸如此类的重要和标准方法时,请记住使用supersuperclass上调用该方法。 与在子类上调用init时相同:

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

    return self;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM