【发布时间】: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