简体   繁体   中英

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. 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:

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

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. I was able to simplify that by taking advantage of UIButton's parentage to UIView to do the same thing. So I took away the nesting view and declared the position of a UIButton like so:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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