繁体   English   中英

UIButton不可点击

[英]UIButton not clickable

我搜索了其他帖子,但找不到解决方案。

我有一个UIButton,无法单击。 当我单击时,它不会变暗。 问题不在于选择器方法。

有人能帮我吗?! 这是代码:

- (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];

}

上面的代码在UIView内部,该UIView通过-loadView()方法通过UIViewController调用。

下面是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是放置此代码的奇怪地方。 它被称为绘制视图,并且可以想象它被称为很多-您不应该在那里构建视图。 例如,当您触摸按钮(重画)时,可能会调用drawRect

如果这是在UIView中,则init可能是放置此代码的更好的位置。

我认为问题是scrollView ..没有转发触摸事件..尝试执行以下操作:

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

暂无
暂无

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

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