iPhone UIButton addTarget:action:forControlEvents:not working

时间:2010-03-13 02:38:29

标签: cocoa-touch iphone-sdk-3.0 uibutton

我看到这里发布了类似的问题,但没有一个解决方案适合我。这是:

我在UIView框架内有一个UIButton实例,它位于另一个UIView框架内,该框架位于主窗口UIView中。即:

UIWindow 
--> UIView (searchView)
  --> UISearchBar (findField)
  --> UIView (prevButtonView)
    --> UIButton (prevButton)
  --> UIView (nextButtonView)
    --> UIButton (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];

方法nextResult:永远不会被调用,按钮的状态在触摸时不会改变,并且没有一个该死的东西我能够做到让它工作。我认为所有这些视图层都存在问题:可能有些东西放在我的按钮上,但我不确定它是什么。

为了信息过载,我发现了一些代码可以打印出我的所有视图信息。这是我为整个视图层次结构所获得的:

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}}

相关部分在UINavigationBar部分上方注明。

有人有什么建议吗?我全力以赴。

感谢阅读。

亚伦。

1 个答案:

答案 0 :(得分:2)

我明白了,所以这是为了子孙后代的交易。

最初,我通过将它们嵌套到UIViews并设置它们的框架属性来定位我的按钮。通过利用UIButton的UIView的父母来做同样的事情,我能够简化这一点。所以我拿走了嵌套视图并宣布了UIButton的位置:

nextButton.frame = CGRectMake(searchView.frame.size.width - 30, 15, 23, 23);

但这并没有解决我的问题;它只是使我的代码更精简。事实证明,这是导致问题的我的框架的X属性的计算。当我像这样重构时:

NSInteger nextX = searchView.frame.size.width - 30;
nextButton.frame = CGRectMake(nextX, 15, 23, 23); 

一切都开始了。去图。

干杯! 亚伦。