![](/img/trans.png)
[英]Why does Interface Builder not show a view controller subclass in the identity inspector?
[英]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等中执行相同的操作。
重要说明:当您覆盖诸如此类的重要和标准方法时,请记住使用super
在superclass
上调用该方法。 与在子类上调用init时相同:
- (id)init {
self = [super init];
if(self) {
//your code
}
return self;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.