gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:43:01 25 4
gpt4 key购买 nike

免责声明:我是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com