简体   繁体   中英

UIButton not clickable

I've searched in other posts but I couldn't find a solution.

I have a UIButton that is not being possible to click. When I click, it doesn't darken. The problem is not about the selector method.

Can someone help me?! Here's the code:

- (void)drawRect:(CGRect)rect
{

    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 436)];

    UIView *detailsView = [[UIView alloc] init];

// I have other components here and after the UIButton

    UIButton *btnOpenPDF = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnOpenPDF setBackgroundImage:[UIImage imageNamed:@"btnVisualizarPdf"] forState:UIControlStateNormal];
    [btnOpenPDF setFrame:CGRectMake(35, totalHeight, 249, 30)];
    btnOpenPDF.userInteractionEnabled = YES;
    [btnOpenPDF addTarget:self action:@selector(openPdf:) forControlEvents:UIControlEventTouchDown];

    [detailsView addSubview:btnOpenPDF];

    [scroll addSubview:detailsView];

    [self addSubview:scroll];

}

The code above is inside an UIView which is called through an UIViewController through the method -loadView().

Below is the method openPdf:

- (IBAction)openPdf:(id)sender{
// Creates an instance of a UIWebView
    UIWebView *aWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320,450)];

// Sets the scale of web content the first time it is displayed in a web view
    aWebView.scalesPageToFit = YES;
    [aWebView setDelegate:self];

//Create a URL object.
    NSURL *urlString = [NSURL URLWithString:[NSString stringWithFormat:@"http://portalarp.com.br/portal/%@",[[[[[[[delegateFunctions products] objectAtIndex:[delegateFunctions selectedProductIndex]] objectForKey:@"ata"] objectForKey:@"nm_arquivo"] objectForKey:@"text"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] stringByReplacingOccurrencesOfString:@"\t" withString:@""]]];

//URL Requst Object
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:urlString];

//load the URL into the web view.
    [aWebView loadRequest:requestObj];

    [self addSubview:aWebView];
    [aWebView release];
}

drawRect is an odd place to put this code. It's called to draw the view, and could conceivably be called a lot -- you shouldn't be building the view there. For example, drawRect might be called when you touch the button (to redraw it)

If this is in a UIView, probably the init is a much better place to put this code.

I think the problem is the scrollView .. is not forwarding the touch event .. try to do the following :

scroll.userInteractionEnabled = YES;
scroll.exclusiveTouch = YES;

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