简体   繁体   English

UIButton子类不响应超级方法?

[英]UIButton subclass not responding to super methods?

I would like to create a UIButton that toggles its image. 我想创建一个UIButton来切换其图像。 Once clicked the image should change to another one and stay that way until clicked again. 单击后,图像应更改为另一图像,并保持这种状态,直到再次单击。

I have added both images to the default and selected states of the button on interface builder. 我已经将两个图像都添加到了界面构建器上按钮的默认和选定状态。

Now, I have created a UIButton subclass. 现在,我创建了一个UIButton子类。 My idea is to intercept the tap to that button and change the button state. 我的想法是拦截对该按钮的点击并更改按钮状态。 So I added this to the UIButton subclass, hoping one of these methods would be triggered by a tap. 因此,我将其添加到UIButton子类中,希望这些方法之一可以通过轻击来触发。

- (void)sendAction:(SEL)action
                to:(id)target
          forEvent:(UIEvent *)event {

  [super sendAction:action to:target forEvent:event];

  // toggle state
  [self setSelected:![self isSelected]];

}


- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents {
  [super sendActionsForControlEvents:controlEvents];

  // toggle state
  [self setSelected:![self isSelected]];

}


- (void)touchesEnded:(NSSet *)touches
           withEvent:(UIEvent *)event {

  [super touchesEnded:touches withEvent:event];
  // toggle state
  [self setSelected:![self isSelected]];
}

None of these methods are triggered. 这些方法均未触发。 Any clues? 有什么线索吗?

I could be barking up the wrong tree (pun partially intended), but I created a Swift project that does what I think you're asking for. 我可能会拨错树(部分意图是双关语),但是我创建了一个Swift项目,该项目可以满足您的要求。 I realize you're using ObjC, but for sake of time on my part I made it in Swift. 我知道您正在使用ObjC,但是为了节省时间,我还是用Swift编写的。 In any case, it'll give you somewhere to start. 无论如何,它将为您提供一个起点。

Here is the repo on GitHub: Click Here 这是GitHub上的仓库: 单击此处
--> Note: You'll need >= Xcode 6 to run the project. ->注意:您需要> = Xcode 6才能运行该项目。

A few things to note in the project: 项目中需要注意的几件事:
1 - Be sure and connect the Button on Main.storyboard to the class you've created. 1-确保将Main.storyboard上的Button连接到您创建的类。
2 - In Swift, the class MyButton: UIButton is the line that implements UIButton for the class. 2-在Swift中, class MyButton: UIButton是为该类实现UIButton的行。 I'm certain there's an ObjC equivalent, this Stack Overflow question may be helpful syntactically: Click Here 我确定有一个ObjC等效项,此堆栈溢出问题在语法上可能会有所帮助: 单击此处

Hopefully some of this is helpful! 希望其中一些有用!

Good luck, 祝好运,
Kyle 凯尔

My guess is that the first two aren't called because the button doesn't have any target s registered with selector s. 我的猜测是前两个未被调用,因为按钮没有向selector s注册任何target The third not being called is a little surprising. 第三个没有被调用有点令人惊讶。

That being said, you should to try [self addTarget:self action:@selector(buttonTapped:) forControlEvents: UIControlEventsTouchUpInside] . 话虽如此,您应该尝试[self addTarget:self action:@selector(buttonTapped:) forControlEvents: UIControlEventsTouchUpInside]

发生这种情况的唯一方法是,如果尚未在UIButton子类上调用addTarget

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM