ios - UIButton 的子类使用 Interface Builder 连接到父 View Controller 的 IBAction?

标签 ios objective-c uibutton interface-builder

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

我创建了一个 NavigationViewController,将两个 ViewControllers 附加到我的 Storyboard,并添加(拖动)一个 UIButton 到第一个 VC .

然后我在两个 VC 之间导航的 UIButton 上创建了一个导航 segue。这按预期工作。

当我尝试使用我创建的 UIButton 的自定义子类时,事情就崩溃了:永远不会触发 segue。

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

UIButton header 如下所示:

#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 等中做同样的事情

重要提示: 当您覆盖重要的标准方法时,请记住使用 super 调用 superclass 上的方法。 这与在子类上调用 init 时所做的相同:

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

    return self;
}

关于ios - UIButton 的子类使用 Interface Builder 连接到父 View Controller 的 IBAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23552871/

相关文章:

ios - 如何使用 Objective-C 在约束条件下绘图

ios - 如何切换 View 和释放先前 View 的内存

ios - NSUserDefaults 中的 Web API 客户端单例与 token

objective-c - 一般学习 Objective-C 和 Xcode 时注意到的好奇心

swift - 如何在结构中使用 UIbutton 进行 segue

ios - 延迟后如何切换回原始按钮图像(obj-c)?

html - 解析多个 HTML 分支 hpple

ios - 从加速度计读数中获取重力

ios - 设置 UIButton 背景的 alpha 但不是标题

ios - AVURLAsset URLAssetWithURL :options: blocks main thread with remote URL?