【问题标题】:Subclass of UIButton connecting to IBAction of parent View Controller using Interface Builder?UIButton的子类使用Interface Builder连接到父视图控制器的IBAction?
【发布时间】:2014-06-26 12:00:50
【问题描述】:

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

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

然后我在UIButton 上创建了一个导航segue,用于在两个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 是什么。

【问题讨论】:

  • 一种更简单的方法可能是覆盖setHighlighted: 以调用 super 并更改 alpha 级别。这将是单个位置中的隔离覆盖,而不是分散在多个方法调用中。通过不覆盖 touchesEnded,您不会在调用目标操作时丢失它(这是 Matteo 的答案所修复的)

标签: ios objective-c uibutton interface-builder


【解决方案1】:

在你的方法中,调用:

[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;
}

【讨论】:

  • 所以当您触摸按钮时,您的代码是否有效?只是segue不起作用?
  • 你在 touchEnded 等中也叫 super 吗?
  • 它起作用了,但是我第一次输入的是 touchesBegan 而不是 touchesEnded,因此它为什么不起作用。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 2016-01-03
  • 2011-07-05
  • 2013-06-05
相关资源
最近更新 更多