[英]iPhone UIButton addTarget:action:forControlEvents: not working
I see there are similar problems posted here, but none of the solutions work for me. 我看到这里也发布了类似的问题,但是没有一种解决方案适合我。 Here goes:
开始:
I have a UIButton instance inside a UIView frame, which is positioned within another UIView frame, which is positioned in the main window UIView. 我在一个UIView框架内有一个UIButton实例,该实例位于另一个UIView框架内,后者又位于主窗口UIView中。 To wit:
以机智:
UIWindow
--> UIView (searchView)
--> UISearchBar (findField)
--> UIView (prevButtonView)
--> UIButton (prevButton)
--> UIView (nextButtonView)
--> UIButton (nextButton)
So far, so good: everything is laid out as I want it. 到目前为止,一切都很好:一切都按照我的意愿进行了布置。 However, the buttons aren't accepting user input of any kind.
但是,这些按钮不接受任何类型的用户输入。 I am using the UIButton method addTarget:action:forControlEvents: and to give you an idea of what I'm doing, here's my code for nextButton:
我正在使用UIButton方法addTarget:action:forControlEvents:并让您了解我在做什么,这是nextButton的代码:
nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
[nextButton setImage:[UIImage imageNamed:@"find_next_on.png"] forState:UIControlStateNormal];
[nextButton setImage:[UIImage imageNamed:@"find_next_off.png"] forState:UIControlStateDisabled];
[nextButton setImage:[UIImage imageNamed:@"find_next_off.png"] forState:UIControlStateHighlighted];
[nextButton addTarget:self action:@selector(nextResult:) forControlEvents:UIControlEventTouchUpInside];
The method nextResult: never gets called, the state of the button doesn't change on touching, and there's not a damned thing I've been able to do to get it working. 方法nextResult:永远不会被调用,触摸时按钮的状态不会改变,并且我没有做过该死的事情来使其正常工作。 I assume that there's an issue with all these layers of views: maybe something is sitting on top of my button, but I'm not sure what it could be.
我认为所有这些视图层都有问题:也许我的按钮上方有东西,但我不确定它可能是什么。
In the interest of information overload, I found a bit of code that would print out all my view info. 为了信息过载,我发现了一些代码,可以打印出我所有的视图信息。 This is what I get for my entire view hierarchy:
这是我整个视图层次结构的结果:
UIWindow
{{0, 0}, {320, 480}}
UILayoutContainerView
{{0, 0}, {320, 480}}
UINavigationTransitionView
{{0, 0}, {320, 480}}
UIViewControllerWrapperView
{{0, 64}, {320, 416}}
UIView
{{0, 0}, {320, 416}}
UIWebView
{{0, 44}, {320, 416}}
UIScroller
{{0, 0}, {320, 416}}
UIImageView
{{0, 0}, {54, 54}}
UIImageView
{{0, 0}, {54, 54}}
UIImageView
{{0, 0}, {54, 54}}
UIImageView
{{0, 0}, {54, 54}}
UIImageView
{{-14.5, 14.5}, {30, 1}}
UIImageView
{{-14.5, 14.5}, {30, 1}}
UIImageView
{{0, 0}, {1, 30}}
UIImageView
{{0, 0}, {1, 30}}
UIImageView
{{0, 430}, {320, 30}}
UIImageView
{{0, 0}, {320, 30}}
UIWebDocumentView
{{0, 0}, {320, 21291}}
UIView
{{0, 0}, {320, 44}}
UISearchBar
{{10, 8}, {240, 30}}
UISearchBarBackground
{{0, 0}, {240, 30}}
UISearchBarTextField
{{5, -2}, {230, 31}}
UITextFieldBorderView
{{0, 0}, {230, 31}}
UIPushButton
{{205, 6}, {19, 19}}
UIImageView
{{10, 8}, {15, 15}}
UILabel
{{30, 7}, {163, 18}}
UIView
{{290, 15}, {23, 23}}
UIButton
{{0, 0}, {0, 0}}
UIImageView
{{-12, -12}, {23, 23}}
UIView
{{260, 15}, {23, 23}}
UIButton
{{0, 0}, {0, 0}}
UIImageView
{{-12, -12}, {23, 23}}
UINavigationBar
{{0, 20}, {320, 44}}
UINavigationButton
{{267, 7}, {48, 30}}
UIImageView
{{0, 0}, {48, 30}}
UIButtonLabel
{{11, 7}, {26, 15}}
UINavigationItemView
{{79, 8}, {180, 27}}
UINavigationItemButtonView
{{5, 7}, {66, 30}}
MBProgressHUD
{{0, 0}, {320, 480}}
UIActivityIndicatorView
{{141, 206}, {37, 37}}
UILabel
{{117, 247}, {86, 27}}
The relevant part is noted above the UINavigationBar section. 相关部分在UINavigationBar部分上方进行了注明。
Anyone have any suggestions? 有人有什么建议吗? I'm all out.
我全力以赴。
Thanks for reading. 谢谢阅读。
Aaron. 亚伦。
I figured it out, so here's the deal for posterity's sake. 我想通了,所以为了后代,这是交易。
Initially, I was positioning my buttons by nesting them into UIViews and setting their frame properties. 最初,我是通过将按钮嵌套到UIViews中并设置其框架属性来定位按钮的。 I was able to simplify that by taking advantage of UIButton's parentage to UIView to do the same thing.
我可以通过利用UIButton对UIView的继承来简化此操作。 So I took away the nesting view and declared the position of a UIButton like so:
因此,我取消了嵌套视图,并声明了UIButton的位置,如下所示:
nextButton.frame = CGRectMake(searchView.frame.size.width - 30, 15, 23, 23);
But that didn't solve my problem; 但这并不能解决我的问题。 it just made my code leaner.
它只是使我的代码更简洁。 Turns out it's the calculation of my frame's X property that was causing the issue.
原来是导致问题的原因是我框架的X属性的计算。 When I refactored like so:
当我这样重构时:
NSInteger nextX = searchView.frame.size.width - 30;
nextButton.frame = CGRectMake(nextX, 15, 23, 23);
Everything started working again. 一切再次开始工作。 Go figure.
去搞清楚。
Cheers! 干杯! Aaron.
亚伦。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.