2012-04-30 240 views
3

我已经在其他帖子中搜索,但我找不到解决方案。UIButton不可点击

我有一个无法点击的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中,通过UIViewController通过方法-loadView()调用。

下面就是该方法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]; 
} 
+0

你确定事件“UIControlEventTouchDown” ..或者你尝试按如通常的“UIControlEventTouchUpInside”?? –

+0

它使用的是UIControlEventTouchDown,因为我在另一篇文章中看到它为某人工作......但是,使用这两种方法仍然无效。 – CainaSouza

+0

你是什么意思的“仍然不工作”..按钮没有突出显示或选择器根本没有调用? –

回答

3

我认为这个问题是滚动视图..不转发触摸事件..尽量做到以下几点:

scroll.userInteractionEnabled = YES; 
scroll.exclusiveTouch = YES; 
+0

不幸的是它也没有工作。 非常奇怪的按钮的行为... – CainaSouza

+1

你尝试直接在scrollView上添加按钮,而无需将其添加到视图中? –

+1

直接添加按钮到scrollView工作! 谢谢! – CainaSouza

7

drawRect是把这个代码的一个奇怪的地方。它被称为绘制视图,可以想象会被称为很多 - 你不应该在那里构建视图。例如,drawRect可能会在您触摸按钮(重画它)时调用

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

+0

即使将代码放在init方法中,该按钮仍然不可点击。 – CainaSouza

+0

有什么事情发生?崩溃,异常等?显示'openPdf声明:' –

+0

另外,您从drawRect中查看所有构建代码,对吧? drawRect中是否还有其他东西?可能不应该是,除非你需要一个不是内置组件的自定义视图。 –