Objective c UI按钮不可单击

Objective c UI按钮不可单击,objective-c,ios,cocoa-touch,uibutton,Objective C,Ios,Cocoa Touch,Uibutton,我在其他帖子中搜索过,但找不到解决方案 我有一个无法点击的UIButton。当我点击时,它不会变暗。 问题不在于选择器方法 有人能帮帮我吗?!代码如下: - (void)drawRect:(CGRect)rect { UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 436)]; UIView *detailsView = [[UIView alloc] init]

我在其他帖子中搜索过,但找不到解决方案

我有一个无法点击的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;

即使将代码放在init方法中,按钮仍然不可单击。有什么事情发生吗?崩溃、异常等?显示
openPdf:
的声明另外,您从drawRect中取出了所有视图建筑代码,对吗?drawRect里还有什么东西吗?可能不应该这样,除非您需要一个不是内置视图组成的自定义视图。什么都不会发生!就像按钮上有一些看不见的部件。openPdf:从未被调用过!正如你所说,我从drawRect方法中取出了所有东西!我用openPdf方法编辑了这篇文章。试着用一个按钮创建一个简单的视图——这样行吗?你确定“UIControlEventTouchDown”事件吗。。或者您尝试像往常一样按“uicontrolEventTouchInside”?它正在使用UIControlEventTouchDown,因为我在另一篇文章中看到它为某人工作。。。但是,使用这两种方法,它仍然不起作用。你说的“仍然不起作用”是什么意思。。按钮没有突出显示,或者选择器根本没有调用?!按钮未高亮显示,因此根本没有调用选择器…确定尝试将detailView背景颜色更改为红色,并检查按钮是否完全位于红色区域内。是否尝试直接在scrollView上添加按钮而不在视图中添加按钮?